这个.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-drive&将我的目标文件夹和源文件夹更改为W-Drive(已映射到驱动器号的网络驱动器)我的代码如下所示,我刚刚更改了目标文件夹的路径和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
)
)
)
答案 0 :(得分:3)
也许吧,也许,你有你的csv文件打开,这将产生错误
“批处理文件错误 - 系统无法找到文件abc.csv”
尝试关闭已打开的CSV文件并运行批处理文件。
因此我浪费了一个小时。
答案 1 :(得分:1)
当从批处理文件访问已在Windows中映射的网络驱动器时,网络驱动器访问权限很可能与运行批处理文件的安全上下文的权限不匹配。
最好在批处理脚本中映射网络驱动器。
net use R: \\servername\folder [password] /USER:domain\username
然后在脚本末尾删除它。
net use R: /delete
答案 2 :(得分:1)
试一试。
@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
您的代码更改:
SET "filename=%%~nxa"
仅使用文件名和扩展名
For /F "usebackq delims=" %%l in ("%%~fa") Do (
已更改为使用正在处理的文件的完整文件路径。包含usebackq
以引用文件的完整路径。
echo(%%l >>"%target_folder%\!SFN!!FileNum!.!SFX!"
添加了遗漏的感叹号
引用所有路径
答案 3 :(得分:0)
我建议您将::SET filename...
更改为rem SET filename...
。
::-comment
实际上是一个损坏的标签,标签往往会终止块,因此你的for...%%a
循环终止于"标签"