Batch file output - batch-file

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%
)
)

Related

How to move files to a new folder or subfolders based on parts of the file name?

I have a folder where lots of .tif files drop into.
They all start with MW_ or SW_ and later also NSSW. So I need to be able to expand from the first two later to NSSW.
I have a batch file already that first moves the files into the folder MW or SW based on the first 2 characters of the file name. Here is my current batch file and it works just fine. But I think I need a second batch file or addition to this to do the below 1 & 2 steps. Please see below, after this code.
REM Sort by First name.
REM This script creates a folder for either the full file name,
REM or if it contains an underscore, the part before the underscore.
REM TODO - Don't copy over existing files.
REM TODO - Move files into Sub folders based on Date in file name "last 8 characters .tif
#echo off
REM Needed because you are working with variables that are immediately called
setlocal enabledelayedexpansion
REM Start of the loop to get all files with a psd or jpg Extension
for %%A in (*.tif *.jpg *.pdf) do (
echo file found %%A
REM Grabs only the file name
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
REM Grabs only the extension
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
REM Moves the file to the folder
echo Moving file %%A to folder !folname!
REM if not exist "%%D\%%A" move "%%A" "!folname!"
if not exist "%%D\%%A" copy "%%A" "!folname!"
REM add the date DDMMYYYY to the end of each file. Name can be 80 characters long.
rem ren "!folname!\%%A" "????????????????????????????????????????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.tif"
)
echo Finished
pause
So here is what I need I guess. A second or 3rd batch file to do the below. I hope, someone can help.
Note: Please keep in mind that if a file exists it renames the copy when moving with xxxx(1).tif, xxx(2).tif, etc. on the end of file name.
Move files now listed in MW folder into new or existing subfolders based on the name of the file from the 4th to 10th character of the file name or from the 4th character to the second or next "_".
Move file in subfolder of the named folder to a sub date named folder based on last 8 characters of file name.
What I need is to then move the files from the MW folder into new or existing subfolders based on the last 7 characters of the file name "the date section".
For example we start with MW files
Files coming in to folder C:\temp\Test_Moving_Files\
MW_VRL5VF10000_6542234_01052016.TIF
MW_Flybuys_677888_01052016.TIF
MW_VRL5VF10000_333443_02052016.TIF
MW_Flybuys_555555_02052016.TIF
MW_goodguys_534535_02052016.TIF
MW_goodguys_222222_02052016.TIF
MW_Flybuys_123443_03052016.TIF
MW_Flybuys_3545555_03052016.TIF
MW_goodguys_444444_03052016.TIF
MW_goodguys_888888_03052016.TIF
Output to subfolders should be sorted to subfolders like below:
MW\VRL5VF10000\01052016\MW_VRL5VF10000_6542234_01052016.TIF
MW\VRL5VF10000\02052016\MW_VRL5VF10000_333443_02052016.TIF
MW\Flybuys\01052016\MW_Flybuys_677888_01052016.TIF
MW\Flybuys\02052016\MW_Flybuys_555555_02052016.TIF
MW\Flybuys\03052016\MW_Flybuys_123443_03052016.TIF
MW\Flybuys\03052016\MW_Flybuys_3545555_03052016.TIF
MW\goodguys\01052016\MW_goodguys_222222_02052016.TIF
MW\goodguys\02052016\MW_goodguys_534535_02052016.TIF
MW\goodguys\03052016\MW_goodguys_444444_03052016.TIF
MW\goodguys\03052016\MW_goodguys_888888_03052016.TIF
It looks the task can be achieved by modifying
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
to
REM Using the file name it separates it into 4 parts using "_" as a delimiter.
for /f "tokens=1-4 delims=_" %%D in ("!fname!") do set "folname=%%D\%%E\%%G"
echo Folder name !folname!
REM Checks for the existence of the folder path. If the folder
REM path does not exist, it creates the complete folder structure.
if not exist "!folname!\*" (
echo Folder !folname! does not exist, creating ...
md "!folname!"
) else (
echo Folder !folname! exists.
)
Instead of using just first substring of underscore delimited file name, the code above uses now first, second and fourth substring. I did not test it!
Command MD can create also multiple folders at once and therefore variable folname can be also a folder path string. Well, I suggest to rename the variable folname in your batch code for example to FolderPath.
Of course you must use folname or better FolderPath also in rest of batch script instead of %%D.

Robocopy script to retrieve documents

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%
)
)

Create a batch file that compares a file name in a text file and a folder and if they match it will copy the file to another folder

I have a text file that contains a file name (test.dll).
I want to check if this file (test.dll) exists in folder #1, and if YES, copy the file to another folder.
Thanks a lot!
Your batch script works correctly but it seems quite redundant. Just clean it, as in the following quotation:
#ECHO OFF
SET "DEST_DIR=%USERPROFILE%\Desktop\[folder_1]"
SET "SEARCH_DIR=%USERPROFILE%\Desktop\[folder_2]"
FOR /F "tokens=*" %%a IN ('type [your_list].txt') DO (
FOR /R "%SEARCH_DIR%" %%f IN (%%a) DO (xcopy /S /I "%%~f" "%DEST_DIR%" 2> NUL)
)
I have added the 2> NUL statement that redirects to nothing the STDERR output. Consider by yourself whether to hold it or not.

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"
)

Rename files in a folder with substring of filename

I never wrote batch files before and now I am having a requirement of renaming every file with YYYYDDMM from the filename aaaYYYYMMDD123456.csv
The code below works if there is only one file, but doesn't work if there are multiple files.
for %%F in (aaa*f.csv) do ( set "name=%%F" ren "%%F" "!name:~3,8!.csv" )
In case of multiple files the last file's YYYYMMDD is renamed for the first file and all the remaining files show the error "A duplicate file name exists, or the file cannot be found"
You can try the code below:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for %%F in (aaa*.csv) do (
set name="%%F"
ren "%%F" "!name:~4,8!.csv"
)

Resources