Reputation: 816
This .bat file splits my csv large files.
Code works perfectly fine when i run on local. My code when on local looks like this
@echo off
setlocal ENABLEDELAYEDEXPANSION
set target_folder=C:\Users\username\Desktop\splitted-files
if not exist %target_folder% mkdir %target_folder%
for /f %%a IN ('dir /b "C:\Users\username\Desktop\split-large-csv-files\*.csv"') do (
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET filename=%%a
REM Edit this value to change the number of lines per file.
SET LPF=3000
REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
SET SFN=splitfile
REM Do not change beyond this line.
SET SFX=!filename!
SET /A LineNum=0
SET /A FileNum=1
For /F "delims==" %%l in (!filename!) Do (
SET /A LineNum+=1
echo %%l >>%target_folder%\SFN!!FileNum!.!SFX!
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
)
endlocal
Pause
Now keeping the file at C-drive & change my target folder and source folder to W-Drive (a network drive that is already mapped to a drive letter) my code looks like this, I have just changed the path for target folder and source in for loop.
It starts giving me an error saying The system cannot find the file abc.csv
@echo off
setlocal ENABLEDELAYEDEXPANSION
set target_folder=W:\Automation\Task\all-file-split
if not exist %target_folder% mkdir %target_folder%
for /f %%a IN ('dir /b "W:\Automation\Task\check-file-split-here\*.csv"') do (
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET filename=%%a
REM Edit this value to change the number of lines per file.
SET LPF=3000
REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
SET SFN=splitfile
REM Do not change beyond this line.
SET SFX=!filename!
SET /A LineNum=0
SET /A FileNum=1
For /F "delims==" %%l in (!filename!) Do (
SET /A LineNum+=1
echo %%l >>%target_folder%\SFN!!FileNum!.!SFX!
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
)
pause
I am not getting where am I going wrong Thanks
Edit So after all the changes my code looks like this
@echo off
setlocal ENABLEDELAYEDEXPANSION
set target_folder=W:\Automation\Task\all-splitted-files
if not exist %target_folder% mkdir %target_folder%
for /f %%a IN ('dir /b "Automation\Task\csv-file\*.csv"') do (
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET "filename=%%~nxa"
REM Edit this value to change the number of lines per file.
SET LPF=800
REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
SET SFN=splitfile
REM Do not change beyond this line.
SET SFX=!filename!
SET /A LineNum=0
SET /A FileNum=1
For /F "usebackq delims==" %%l in ("%%~fa") Do (
SET /A LineNum+=1
echo %%l >>%target_folder%\!SFN!!FileNum!.!SFX!
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
)
Upvotes: 2
Views: 3326
Reputation: 2286
Maybe, Just maybe, you have your csv file open , which will generate the error
"Batch file error - The system cannot find the file abc.csv"
Try closing the opened CSV file and run you batch file.
I had wasted an Hour because of this.
Upvotes: 5
Reputation: 70933
Give this a try.
@echo off
setlocal enableextensions ENABLEDELAYEDEXPANSION
SET "target_folder=W:\Automation\Task\all-file-split"
if not exist "%target_folder%" mkdir "%target_folder%"
SET "source_folder=W:\Automation\Task\check-file-split-here"
for %%a IN ("%source_folder%\*.csv") do (
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET "filename=%%~nxa"
REM Edit this value to change the number of lines per file.
SET "LPF=3000"
REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
SET "SFN=splitfile"
REM Do not change beyond this line.
SET "SFX=!filename!"
SET /A "LineNum=0"
SET /A "FileNum=1"
For /F "usebackq delims=" %%l in ("%%~fa") Do (
SET /A "LineNum+=1"
echo(%%l >>"%target_folder%\!SFN!!FileNum!.!SFX!"
if !LineNum! EQU !LPF! (
SET /A "LineNum=0"
SET /A "FileNum+=1"
)
)
)
pause
Changes from your code:
SET "filename=%%~nxa"
Uses only file name and extension
For /F "usebackq delims=" %%l in ("%%~fa") Do (
Changed to use the full file path to the file being processed. Included usebackq
to quote the full path to the file.
echo(%%l >>"%target_folder%\!SFN!!FileNum!.!SFX!"
Added a missing exclamation point
All paths quoted
Upvotes: 1
Reputation: 2495
When accessing a network drive that is already mapped in Windows from a batch file, the permissions of the network drive access will most likely not match the permissions of the security context which the batch file is running under.
You are better off mapping the network drive within the batch script.
net use R: \\servername\folder [password] /USER:domain\username
And then remove it at the end of the script.
net use R: /delete
Upvotes: 1
Reputation: 80033
I'd suggest you change ::SET filename...
to rem SET filename...
.
The ::-comment
is actually a broken label and labels tend to terminate blocks, hence your for...%%a
loop terminates at the "label"
Upvotes: 0