Robocopy script to retrieve documents - batch-file

I have the following batch file that connects to a network drive and retrieves a list of documents contained in the e:\cn_documents.csv file to the e:\data directory and any files it can't find it sticks it in the doesnotexist.csv file.
The problem with the batch job is it took 20 hours to retrieve 200000 documents. I was told that Robocopy would be quicker. Would anyone have a robocopy script that would do the same?
#echo off
net use \\10.10.10.1\ipc$ /d
net use \\10.10.10.1\ipc$ /user:company\joebloggs Password1
SET destfolder=e:\data
SET DoesNotExistList="E:\DoesNotExist_CN.txt"
REM Reset the not exist list so it doesn't pile up from multiple runs.
ECHO The following Contract Note files were not found > %DoesNotExistList%
FOR /F "delims=" %%a IN (e:\cn_documents.csv) DO (
REM Check if the file exists.
IF EXIST "%%a" (
REM It does exist, copy to the destination.
COPY "%%a" "%destfolder%\%%~nxa"
) ELSE (
REM It does not exist, note the file name.
ECHO %%a>>%DoesNotExistList%
)
)

Related

CMD Batch script to sort files

I have an application which saves files to a FTP folder that I sync to my PC which contains multiple JPG and MP4 files, named in the following format:
ARC20170510151547.jpg
ARC20170510151549.mp4
What I'm trying to do is:
Copy the files from my FTP to my PC
Sort the files into folders based on the day they were created
Delete files from the FTP older than 14 days
Delete files from the PC older than 1 month
Using WinSCP to connect to FTP with the following code, I am able to download all the files to my local drive:
"c:\program files (x86)\winscp\winscp.com" /ini=nul /command ^
"open ftp://[username]:[password]#[ipaddress]/" ^
"synchronize local f:\[localpath]\ /[remotepath]/ " ^
"exit"
Then I need to sort the files. Here is where I am stuck. I'm think I know the commands but I am unsure how to use the 'tokens' and 'delims' to get it to work how I want.
#echo
for %%a in (*.*) do (
echo processing "%%a"
for /f "tokens=1 delims=" %%a in ("%%~nxa") do (
md "%%b-%%c" 2>nul
move "%%a" "%%b-%%c" >nul
)
)
pause
As I know the filename format isn't going to change, one thing I have considered is adding some special characters to the filename, maybe using the 'ren' command. I could then use those special characters as search delims but, again, I'm struggling how to best proceed.
Removing local files older than 30 days is easy using the following script
forfiles -p "f:\[localpath]" -s -m *.* -d <number of days> -c "cmd /c del #path"
However, the WinSCP 'RM' command I am using doesn't appear to be working. It returns an error "no file matching '*<14D' found"
"rm /[filepath]/*<14D" ^
Any help, advice and guidance would be very gratefully received!
Since there is no delimiter between the date elements you need substrings,
substrings do only work with normal variables and in a (code block) you need delayed expansion
It's unclear if you want folders with year-month or month-day, select the proper part in the batch and comment/uncomment the Rem:
With extensions enabled md can create the structure YY-MM\DDin one step.
So you can move directly to that folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~nA
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
Rem YY-MM\DD
md "!YY!-!MM!\!DD!" 2>nul
move "%%a" "!YY!-!MM!\!DD!" >nul
)
pause
#LotPings! Thank you for the script. This does almost what I want it to do. I have modified the script as below which moves the files into folders based on the Day.
So, each day, maybe 100 - 200 files are generated. So I do not mind having a folder for each day. Once the files are moved into their respective "Day" folder, what I'd then like to do is create a SubFolder "!YY!-!MM!" and then move the "!DD!" Folders into the "!YY!-!MM!" folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~a
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
md "!DD!" 2>nul
md "!YY!-!MM!" 2>nul
move "%%a" "!DD!" >nul
)
pause

Locate multiple files with the same extension batch script

I'm writing this batch script but when it gets to looking fore if all files of a file extension exists, it goes and does the else even those the file exists. Thanks for the help in advance!
#echo off
if not exist C:\Users\%USERNAME%\Desktop\workBackup (
::creates new folder 'workBackup'
mkdir C:\Users\%USERNAME%\Desktop\workBackup
)
set TIMESTAMP=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%
if not exist C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP% (
::creates new folder to put work into
mkdir C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%
)
if not exist C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\logs (
::creates logs folder
mkdir C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\logs
)
if exist C:\User\%USERNAME%\Documents\workspace\out\log\*.xlsx (
::moves all xlsx files to workBackup folder
move "C:\User\%USERNAME%\Documents\workspace\out\log\"*.xlsx C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\
) else (
::exits if file no files are found
echo "Files not found"
exit /b
)
if exist C:\User\%USERNAME%\Documents\workspace\out\log\*.xlsx (
::moves all log files to workBackup folder
move "C:\Users\%USERNAME%\Documents\workspace\out\log\"*.log C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\logs
) else (
::exits if file no files are found
echo "Files not found"
exit /b
)
There are several things to mention
the date format is a per user setting, better get date from wmic
when creating a folder structure the deepest one will imply the other
don't test, make and suppress an eventual error msg.
reduce errorprone redundancy by creating vars
put double quotes around the whole path not only part of it
#echo off
for /f %%A in ('wmic os get LocalDateTime^|findstr ^^[0-9]') do Set _DT=%%A
Set yy=%_DT:~0,4%&Set MM=%_DT:~4,2%&Set dd=%_DT:~6,2%
Set "WBu=C:\Users\%USERNAME%\Desktop\workBackup"
Set "WBuDT=%WBu%\%dd%%MM%%yy%"
:: or reverse day and month %MM%%dd%%yy%
Set "WSpOL=C:\Users\%USERNAME%\Documents\workspace\out\log"
mkdir "%WBuDT%\logs" 2>NUL
::moves all xlsx files to workBackup folder
move "%WSpOL%\*.xlsx" "%WBuDT%\" 2>NUL||(Echo "%WSpOL%\*.xlsx" not found&Exit /b)
::moves all log files to workBackup folder
move "%WSpOL%\*.log" "%WBuDT%\logs\" 2>NUL||(Echo "%WSpOL%\*.log" not found&Exit /b)

Batch File to Check for Directory and copy it over if it does not exist

I am trying to create a batch file that will check a list of computers for a specific directory and if the directory exist pop-up a message on the screen.
If the directory does not exist then copy it to the remote system.
I have not been able to find any good examples.
The batch file I am working on does not function well at all. It does no tell you when the directory exist, it does not write to the text file and when it goes to copy I am getting invalid path.
This is where I am at now. Any help would be appreciated as this is my 1st attempt at writing a batch file
#echo off
for /F %%A in (machines.txt) do (
if exist "\\%%A\C$\temp\test\" (
echo %%A Directory Exits >>exists.txt
) else (
XCOPY "C:\temp\test\" "\\%%a\c$\Temp\test\" /D /E /C /R /I /K /Y /Q
)
Since the code above was not working I decided to start over and make sure the batch file could find the directory called Test.
When I run the batch file it creates the missing.txt file instead of the exists.txt file which would indicate that it found the directory. I am not sure why it is not finding the directory or maybe it is but my code is incorrect?
This is what I ran
#echo off
for /F %%A in (machines.txt) do (
if exist "\\%%A\C:\Windows\Test" (
echo %%A : FOUND >>exists.txt
) else (
echo %%A : MISSING >>missing.txt
)
)
Pause

Batch file output

I have a batch file that reads through a text file to obtain document locations, it then goes and copies the physical file to a local folder. What I need to include is if the physical file doesn't exist I need to output the document details from the text file to another file so I have a list of missing document. I hope this makes sense.
Here's my batch file contents
SET destfolder=e:\data
FOR /F "delims=" %%a IN (e:\cn_documents.csv) DO COPY "%%a" "%destfolder%\%%~nxa"
It sounds like all you need is to simply check if the file exists before copying. This can be done with an IF EXIST condition.
SET destfolder=e:\data
SET DoesNotExistList="E:\DoesNotExist.txt"
REM Reset the not exist list so it doesn't pile up from multiple runs.
ECHO The following files were not found > %DoesNotExistList%
FOR /F "delims=" %%a IN (e:\cn_documents.csv) DO (
REM Check if the file exists.
IF EXIST "%%a" (
REM It does exist, copy to the destination.
COPY "%%a" "%destfolder%\%%~nxa"
) ELSE (
REM It does not exist, note the file name.
ECHO %%a>>%DoesNotExistList%
)
)

Copy PDF's to Folder using batch

I want to copy several unique PDF files to a unique folder.
The folders already exists.
For example
C:\Document\240C03881_10.pdf Copy this one to : C:\Endresult\240C03881\240C03881_10.pdf
C:\Document\240C03882_10.pdf Copy this one to : C:\Endresult\240C03882\240C03882_10.pdf
C:\Document\240C03883_10.pdf Copy this one to : C:\Endresult\240C03883\240C03883_10.pdf
The script should only read the first 9 digits. The script may not read _10.
Example,
Script sees 240C03881_10. But read it as 240C03881. The script is going to look if the 240C03881 folder exists. If not, the script ends/ignores it. If it does exists, it places the .pdf to its corresponding location.
This is the script i currently have, but nothing happens.. anyone? :
#echo off
setlocal EnableDelayedExpansion
rem Process all .pdf files
for %%a in (*.pdf) do (
rem Get just the file name, ie: "888123AA"
set fileName=%%~Na
rem Using the file name minus two last chars, ie: "888123"
rem get the default folder with that name
for /D %%b in (*-!fileName:~0,-3!-*) do (
rem And copy the file to that folder
copy "%%a" "%%b"
)
)
for %%p in (*.pdf) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "c:\endresult\%%~n\%%~nxp"
)

Resources