批处理文件错误 - 系统找不到文件 abc.csv

Pen*_*nny 2 cmd batch-file

这个 .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
Run Code Online (Sandbox Code Playgroud)

现在将文件保留在 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
Run Code Online (Sandbox Code Playgroud)

我不明白我哪里错了谢谢

编辑 所以在所有更改之后我的代码看起来像这样

@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
        )

    )
)
Run Code Online (Sandbox Code Playgroud)

Hit*_*ua1 5

也许,只是也许,您打开了 csv 文件,这会生成错误

“批处理文件错误 - 系统找不到文件 abc.csv”

尝试关闭打开的 CSV 文件并运行批处理文件。

我因此浪费了一个小时。