-1

I'm trying to copy the latest file in one network location to another network location and rename the file. The source path is \test\BI\reports\2023 The destination path is \PM\test\reports the destination file name is BI-1234_wPC.xls

I used the code from ashipfl in the following thread text.

pushd "\\fipr01\SNAP"
for /F "delims=" %%F in ('
    dir /B /O:D /T:C "*.tmp"
') do (
    set "NEWEST=%%F"
)
copy /Y "%NEWEST%" "C:\Users\ddtacopy\Desktop\AutoCopy\SNAP.txt"
popd

The expected outcome should be \PM\test\reports\BI-1234_wPC.xls

Unfortunately, the file is not being copied at all. Any help will be appreciated.

cperez
  • 1
  • You need to work harder on your question content. Please [Edit] your question to carify why you are telling us that the source is ```\test\BI\reports\2023```, when your code shows it as ```\\fipr01\SNAP```, and your destination says ```\PM\test\reports```, when your code shows it as ```C:\Users\ddtacopy\Desktop\AutoCopy```. Also why have you coded the destination filename as ```SNAP.txt``` even though you want it to be ```BI-1234_wPC.xls```. – Compo Aug 18 '23 at 22:03
  • There can be used a batch file with the single command line `@for /F "eol=| delims=" %%I in ('dir "\\fipr01\SNAP\test\BI\reports\2023\*" /A-D /B /O-D /TW 2^>nul') do @copy "\\fipr01\SNAP\test\BI\reports\2023\%%I" "\\fipr01\SNAP\PM\test\reports\BI-1234_wPC.xls" & exit /B` or whatever are the correct directory paths and the correct destination file name as that is really not clearly explained in the question. – Mofi Aug 19 '23 at 14:43
  • It is also not explained if the batch file is executed by a scheduled task in which case a batch file would not be needed at all as there can be executed directly `%SystemRoot%\System32\cmd.exe` with the arguments `/D` and `/C` and the command line with the __FOR__ and the __COPY__ and the __EXIT__ command running in background one more `%SystemRoot%\System32\cmd.exe` for execution of __DIR__. Open a command prompt and run `cmd /?`, `dir /?`, `for /?`, `copy /?` and `exit /?` and read each output help carefully and completely. – Mofi Aug 19 '23 at 14:46
  • If a scheduled task is indeed used, is the built-in local __SYSTEM__ account configured as account for the task? This account has by default no permissions to access files and folders on network resources in anyway, except the shared folder(s) have read/write permissions set for __EVERYONE__. The scheduled task must be run using the UNC paths with your account and your credentials (user name and password) saved encrypted by Windows on saving the scheduled task. – Mofi Aug 19 '23 at 14:49
  • Thank you for the replies. The code I am using is below. I have tried it running the batch manually and scheduled and both times it fails to copy the file. `pushd "\\test\BI\reports\2023" for /F "delims=" %%F in (' dir /B /O:D /T:C "*.tmp" ') do ( set "NEWEST=%%F" ) copy /Y "%NEWEST%" "\PM\test\reports\BI-1234_wPC.xls" popd` – cperez Aug 21 '23 at 12:52

0 Answers0