这个 .bat 文件拆分了我的 csv 大文件。
当我在本地运行时,代码工作得很好。 我在本地的代码是这样的
@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
现在将文件保存在 C 盘并将我的目标文件夹和源文件夹更改为 W 盘(已经映射到驱动器号的网络驱动器)我的代码如下所示,我刚刚更改了目标路径for循环中的文件夹和源。
它开始给我一个错误提示 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
我不知道哪里错了 谢谢
编辑 所以在所有更改之后我的代码看起来像这样
@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
)
)
)
最佳答案
也许,只是也许,您打开了您的 csv 文件,这将产生错误
“批处理文件错误 - 系统找不到文件 abc.csv”
尝试关闭打开的 CSV 文件并运行批处理文件。
我因此浪费了一个小时。
关于batch-file - 批处理文件错误 - 系统找不到文件 abc.csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27030201/