Thursday, March 19, 2015

windows - Moving pdf files to parent folder from subfolder with variable names


I'm trying to create a bat files we can use daily to copy or move PDF files from subfolders (names of folder vary daily) to the parent folder. I have tried the below:


First tried to just copy the files using this..didn't work


copy "Y:\Print OPS\Annuity Ops\*\*.pdf" "Y:\Print OPS\Annuity Ops"

Next I tried to make a list of the files to be copied and use that to copy them, the list is created by the files aren't copied.


Echo %date%      Sweep Time = %time%       File count = %cnt% > 000_testpdf.txt
echo.>>000_testpdf.txt
dir /b /s *.pdf, /O:N >> 000_testpdf.txt
set logfile=MSOffice_PDF.log
dir /b /s *.pdf, > 000_testpdf.txt
for /f "delims=" %%i in (000_testpdf.txt) do echo D|xcopy "Y:\Print OPS\Annuity Ops\%%i" "Y:\Print OPS\Annuity Ops%%i" /i /z /y

Answer



                               v - disallowed
copy "Y:\Print OPS\Annuity Ops\*\*.pdf" "Y:\Print OPS\Annuity Ops"
^ allowed

Wild characters * are allowed only in last part of a path in Windows. Next code snippet could help:


@ECHO OFF
SETLOCAL EnableExtensions
set "_parent=Y:\Print OPS\Annuity Ops"
for /D %%G in ("%_parent%\*") do (
if exist "%%~G\*.pdf" (
echo copy /B "%%~G\*.pdf" "%_parent%\"
) else (
echo nothig to copy "%%~G\*.pdf" "%_parent%\"
)
)

Note that above code merely displays commands to perform for debugging purposes. Replace echo copy /B with operational copy /B no sooner than debugged. You could also remove all else branch.


Resources (required reading):


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...