0

If i play this batch file, it send me error message such like this, "the system cannot find the file specified"

I searched a lot, but i do not know ...

i saw this article already, but it did not work.

System cannot find the path specified

echo off
echo **** TEST BACK UP BATCH FILE STARTS  ****
setlocal enabledelayedexpansion
:LOOP
  FOR %%f in (C:\Web\FTP\A\*.txt) do (
    xcopy /Y %%f "C:\Web\FTP\A\B\"
    SET "file=%%~nf"
    SET "mydate=!date:~10,4!!date:~7,2!!date:~4,2!"
    SET "mytime=!time:~0,2!!time:~3,2!!time:~6,2!"
    ren "C:\Web\FTP\A\B\%file%.txt" "!file!_!mydate!!mytime!.txt"
    move /Y "C:\Web\FTP\A\!file!.txt" "C:\Web\FTP\A\C\in\"
  )
timeout /t 15
goto LOOP
Seth Hong
  • 23
  • 1
  • 4
  • 2
    you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) for your `file` variable (if you ran your code with `echo on`, you would have seen, `%file%` is obviously empty). But there is no need for that variable, you can use `%%~nf` instead. – Stephan Apr 18 '18 at 20:11
  • You do not need the `IF EXIST`. If the `FOR` command does not find any files that match the path with that specific file mask it will not execute any of the code inside the parenthesized code block. – Squashman Apr 18 '18 at 20:31
  • Possible duplicate of [Variables in batch not behaving as expected](https://stackoverflow.com/questions/30282784/variables-in-batch-not-behaving-as-expected) – Squashman Apr 18 '18 at 20:33
  • You also do not need the rename command. You can do the rename when you copy the file as well. – Squashman Apr 18 '18 at 20:36
  • @Squashman thnx for your advice. I fixed it a little, but I wonder, it works but the error message still happen ,, I'm confused.. – Seth Hong Apr 18 '18 at 21:19
  • @Stephan thnx you for your answer. i forgot that. – Seth Hong Apr 18 '18 at 21:25
  • I think there is problem in command of ren , i mean , im finding way to set path... but the error message is in file. – Seth Hong Apr 18 '18 at 21:26
  • @Seth, Did you read and take the advice Stephan gave in his comment? – Compo Apr 18 '18 at 21:27
  • @compo yes i rewrite to SET "file=%%~nf" – Seth Hong Apr 18 '18 at 21:34
  • @compo do you think is there anything do i have to add more? – Seth Hong Apr 18 '18 at 21:36
  • @SethHong, read the link Stephan posted. You are completely misunderstanding the problem. – Squashman Apr 18 '18 at 21:40
  • @Squashman OMG gegus ! , I really really translate it exactly, and i read it, and i added setlocal enabledelayedexpansion. and then it worked !! , Thank you very much!, English is a second language so ,, i made mistake a lot,, i love google and stackoverflow ,this site – Seth Hong Apr 18 '18 at 21:48
  • @Stepahn thank you! you really helped me and sorry to not seeing exactly that. – Seth Hong Apr 18 '18 at 21:49
  • you guys have a good day :) , i really really appreciate it. – Seth Hong Apr 18 '18 at 21:50

1 Answers1

0

Here are all the changes suggested in the comments above.

echo off
setlocal enabledelayedexpansion
echo **** TEST BACK UP BATCH FILE STARTS  ****
:LOOP
FOR %%G in (C:\Web\FTP\A\*.txt) do (
    SET "mydate=%date:~10,4%%date:~7,2%%date:~4,2%"
    SET "mytime=%time:~0,2%%time:~3,2%%time:~6,2%"
    xcopy /Y "%%~G" "C:\Web\FTP\A\B\%%~nG_!mydate!!mytime!.txt"
    move /Y "%%~G" "C:\Web\FTP\A\C\in\"
)
timeout /t 15
goto LOOP
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • thank you but i read the site which Stephan uploaded and i fixed it. thank you for helping me. have a good day. – Seth Hong Apr 18 '18 at 21:55
  • you are very kind :) – Seth Hong Apr 18 '18 at 21:55
  • @SethHong, I posted this code because you still were not understanding Stephan's comment about not needing to create a variable named file. I also updated the code with other efficiencies. You are also not understanding how StackOverFlow works because you most likely did not read the tour. This is a Question and Answer site. Which means an answer should get posted below your question. You do not update your Question with the Answer. If you have the answer to your own question, then you post the answer as an answer. – Squashman Apr 18 '18 at 22:00
  • oh thnx I got it. Now I can understand this site more better. I'm second time to use this website. I have found the code when i have an error but this time was very hard. if i ask again by this site, i will follow your word. thank you. – Seth Hong Apr 18 '18 at 22:08