由文件位置引起的.bat文件错误

时间:2018-01-29 14:09:55

标签: batch-file

我有以下代码将三个不同的.txt文件合并到一个.txt文件中,根据文件内容重命名新文件并删除旧文件:

 @Echo Off
Set _Path=%~dp0
PushD %_Path%
For /F "Tokens=1 Delims=" %%a In ('Dir /A-D /B *.txt') Do (
Echo %%a
For /F "Usebackq Tokens=1 skip=1 Delims=" %%I In ("%%a") Do >>"%temp%\%%a" Echo.%%I
Move /Y "%temp%\%%a" .\
)
PopD

COPY /b %~dp0\PreSet.txt + %~dp0\Header.txt + %~dp0\Item.txt %~dp0\Output.txt

setlocal enableDelayedExpansion
set "dir_with_files=%~dp0"
for %%# in ("%dir_with_files%\*Output*") do (
  set "date_="
  set "number="
  for /f "usebackq skip=1 tokens=3,4,5,6 delims=. " %%a in ("%%#") do (
     if not defined date_ (
      set "date_=%%a%%b%%c"
      set "number=%%d"
     )
  )
  ren "%%~f#" "!date_!_BOM_!number!%%~x#"
)
endlocal


cd "%~dp0"
DEL PreSet.txt
DEL Header.txt
DEL Item.txt

现在我遇到程序部分工作的问题,意味着取决于.bat文件的存储位置+三个txt。文件程序是否有效。我在这里有点迷失,因为设置总是一样的(意味着.bat程序+ 3个文件总是在同一个位置):

Debugging

有人可以帮忙吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

它不起作用的一个例子(c:\ Users \ dummy \ Desktop \ New folder):

    c:\Users\dummy\Desktop\New folder>For /F "Tokens=1 Delims=" %a In ('Dir /A-D /B *.txt') Do (
Echo %a
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("%a") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\%a"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\%a" .\
)

c:\Users\dummy\Desktop\New folder>(
Echo Header.txt
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("Header.txt") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\Header.txt"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\Header.txt" .\
)
Header.txt

c:\Users\dummy\Desktop\New folder>Echo.1CS01                               13026715                    26.10.2017                                                                                           905707                                                                                    1 5    1                   V441                                                                                                                                                                                                                                                                                            1>>"C:\Users\dummy\AppData\Local\Temp\Header.txt"
        1 file(s) moved.

c:\Users\dummy\Desktop\New folder>(
Echo Item.txt
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("Item.txt") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\Item.txt" .\
)
Item.txt

c:\Users\dummy\Desktop\New folder>Echo.2                          000001                                     1                                                 N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

c:\Users\dummy\Desktop\New folder>Echo.2                          906092                                     126932,8                                          N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

c:\Users\dummy\Desktop\New folder>Echo.2                          000002                                     1                                                 N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

c:\Users\dummy\Desktop\New folder>Echo.2                          000004                                     3274069,96                                        N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

c:\Users\dummy\Desktop\New folder>Echo.2                          310163                                     293811                                            N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"
        1 file(s) moved.

c:\Users\dummy\Desktop\New folder>(
Echo PreSet.txt
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("PreSet.txt") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\PreSet.txt"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\PreSet.txt" .\
)
PreSet.txt

c:\Users\dummy\Desktop\New folder>Echo.0CREATE_BOM  710BATCH_AG    20171115X 1>>"C:\Users\dummy\AppData\Local\Temp\PreSet.txt"
        1 file(s) moved.

c:\Users\dummy\Desktop\New folder>PopD

c:\Users\dummy\Desktop\New folder>COPY /b c:\Users\dummy\Desktop\New folder\\PreSet.txt + c:\Users\dummy\Desktop\New folder\\Header.txt + c:\Users\dummy\Desktop\New folder\\Item.txt c:\Users\dummy\Desktop\New folder\\Output.txt
The system cannot find the file specified.

c:\Users\dummy\Desktop\New folder>setlocal enableDelayedExpansion

c:\Users\dummy\Desktop\New folder>set "dir_with_files=c:\Users\dummy\Desktop\New folder\"

c:\Users\dummy\Desktop\New folder>for %# in ("c:\Users\dummy\Desktop\New folder\\*Output*") do (
set "date_="
 set "number="
 for /F "usebackq skip=1 tokens=3,4,5,6 delims=. " %a in ("%#") do (if not defined date_ (
set "date_=%a%b%c"
 set "number=%d"
) )
 ren "%~f#" "!date_!_BOM_!number!%~x#"
)

c:\Users\dummy\Desktop\New folder>endlocal

c:\Users\dummy\Desktop\New folder>cd "c:\Users\dummy\Desktop\New folder\"

c:\Users\dummy\Desktop\New folder>DEL PreSet.txt

c:\Users\dummy\Desktop\New folder>DEL Header.txt

c:\Users\dummy\Desktop\New folder>DEL Item.txt

它可以工作的一个例子(c:\ Users \ dummy \ Desktop \ test1):

C:\Users\dummy\Desktop\test1>BOM_Creation_Tool_v3.bat

C:\Users\dummy\Desktop\test1>Set _Path=C:\Users\dummy\Desktop\test1\

C:\Users\dummy\Desktop\test1>PushD C:\Users\dummy\Desktop\test1\

C:\Users\dummy\Desktop\test1>For /F "Tokens=1 Delims=" %a In ('Dir /A-D /B *.txt') Do (
Echo %a
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("%a") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\%a"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\%a" .\
)

C:\Users\dummy\Desktop\test1>(
Echo Header.txt
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("Header.txt") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\Header.txt"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\Header.txt" .\
)
Header.txt

C:\Users\dummy\Desktop\test1>Echo.1CS01                               13026715                    26.10.2017                                                                                           905707                                                                                    1 5    1                   V441                                                                                                                                                                                                                                                                                            1>>"C:\Users\dummy\AppData\Local\Temp\Header.txt"
        1 file(s) moved.

C:\Users\dummy\Desktop\test1>(
Echo Item.txt
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("Item.txt") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\Item.txt" .\
)
Item.txt

C:\Users\dummy\Desktop\test1>Echo.2                          000001                                     1                                                 N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

C:\Users\dummy\Desktop\test1>Echo.2                          906092                                     126932,8                                          N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

C:\Users\dummy\Desktop\test1>Echo.2                          000002                                     1                                                 N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

C:\Users\dummy\Desktop\test1>Echo.2                          000004                                     3274069,96                                        N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"

C:\Users\dummy\Desktop\test1>Echo.2                          310163                                     293811                                            N                                                                                                                                                                                                   X                                                                                                     1                                                                                                                                                                1>>"C:\Users\dummy\AppData\Local\Temp\Item.txt"
        1 file(s) moved.

C:\Users\dummy\Desktop\test1>(
Echo PreSet.txt
 For /F "Usebackq Tokens=1 skip=1 Delims=" %I In ("PreSet.txt") Do Echo.%I1>>"C:\Users\dummy\AppData\Local\Temp\PreSet.txt"
 Move /Y "C:\Users\dummy\AppData\Local\Temp\PreSet.txt" .\
)
PreSet.txt

C:\Users\dummy\Desktop\test1>Echo.0CREATE_BOM  710BATCH_AG    20171115X 1>>"C:\Users\dummy\AppData\Local\Temp\PreSet.txt"
        1 file(s) moved.

C:\Users\dummy\Desktop\test1>PopD

C:\Users\dummy\Desktop\test1>COPY /b C:\Users\dummy\Desktop\test1\\PreSet.txt + C:\Users\dummy\Desktop\test1\\Header.txt + C:\Users\dummy\Desktop\test1\\Item.txt C:\Users\dummy\Desktop\test1\\Output.txt
C:\Users\dummy\Desktop\test1\\PreSet.txt
C:\Users\dummy\Desktop\test1\\Header.txt
C:\Users\dummy\Desktop\test1\\Item.txt
        1 file(s) copied.

C:\Users\dummy\Desktop\test1>setlocal enableDelayedExpansion

C:\Users\dummy\Desktop\test1>set "dir_with_files=C:\Users\dummy\Desktop\test1\"

C:\Users\dummy\Desktop\test1>for %# in ("C:\Users\dummy\Desktop\test1\\*Output*") do (
set "date_="
 set "number="
 for /F "usebackq skip=1 tokens=3,4,5,6 delims=. " %a in ("%#") do (if not defined date_ (
set "date_=%a%b%c"
 set "number=%d"
) )
 ren "%~f#" "!date_!_BOM_!number!%~x#"
)

C:\Users\dummy\Desktop\test1>(
set "date_="
 set "number="
 for /F "usebackq skip=1 tokens=3,4,5,6 delims=. " %a in ("C:\Users\dummy\Desktop\test1\\Output.txt") do (if not defined date_ (
set "date_=%a%b%c"
 set "number=%d"
) )
 ren "C:\Users\dummy\Desktop\test1\Output.txt" "!date_!_BOM_!number!.txt"
)

C:\Users\dummy\Desktop\test1>(if not defined date_ (
set "date_=26102017"
 set "number=905707"
) )

C:\Users\dummy\Desktop\test1>(if not defined date_ (
set "date_=1NX"
 set "number=1"
) )

C:\Users\dummy\Desktop\test1>(if not defined date_ (
set "date_=126932,8NX"
 set "number=1"
) )

C:\Users\dummy\Desktop\test1>(if not defined date_ (
set "date_=1NX"
 set "number=1"
) )

C:\Users\dummy\Desktop\test1>(if not defined date_ (
set "date_=3274069,96NX"
 set "number=1"
) )

C:\Users\dummy\Desktop\test1>(if not defined date_ (
set "date_=293811NX"
 set "number=1"
) )

C:\Users\dummy\Desktop\test1>endlocal

C:\Users\dummy\Desktop\test1>cd "C:\Users\dummy\Desktop\test1\"

C:\Users\dummy\Desktop\test1>DEL PreSet.txt

C:\Users\dummy\Desktop\test1>DEL Header.txt

C:\Users\dummy\Desktop\test1>DEL Item.txt