Script:
pushd "\\server1\share\Data\"
for %%p in (*.pdf) do if /i ".pdf"=="%%~xp" for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" ""\\server2share\data\%%~n\%%~nxp" dir >> \\server\share\logfile.txt
)
I want to log the output everytime this command runs. The script copies pdf's based on name. But when i run the script. No log file is created.
EDITED to adapt to comments
pushd "\\server1\share\Data\"
set "logFile=\\server\share\logfile.txt"
for %%p in (*.pdf) do if /i ".pdf"=="%%~xp" for /f "tokens=1 delims=_" %%n in ("%%~np") do (
if not exist ""\\server2share\data\%%~n\" (
>>"%logFile%" echo SKIPPED : "%%~nxp"
) else (
copy "%%~fp" "\\server2share\data\%%~n\%%~nxp"
if errorlevel 1 (
>>"%logFile%" echo FAILED : "%%~nxp"
) else (
>>"%logFile%" echo COPIED : "%%~nxp"
)
)
)
popd
You have a dir command in a strange place, changed to echo. And, i'm not sure all the paths are right.
pushd "\\server1\share\Data\"
for %%p in (*.pdf) do if /i ".pdf"=="%%~xp" for /f "tokens=1 delims=_" %%n in ("%%~np") do (
md "\\server2share\data\%%~n" 2>nul
copy "%%~fp" "\\server2share\data\%%~n\%%~nxp"
dir >> \\server\share\logfile.txt
)
(untested)
I'm assuming the way your scrip is arranged from your original post.
If you want to execute a sequence of separate commands, they need to be separated by & or be on separate lines.
You had an extra " in your copy command
Inserted a md command with error-message-suppression to ensure the destination directory exists.
This will create a log of what is shown on the cmd screen while it is running, but you will not see any output on the screen. You can use a tee filter if you also need to see the screen output.
mybatch.bat>file.log
Related
I have a script that successfully scans a specified directory for a list of files, copies the files to a destination folder and generates a log of all files not found in the source. I had to alter this script to include net use in order to map a network drive. Since doing so the script no longer generates the error log as it did before.
I'm very new to this and can't find any information on why this may be happening. Can somebody please help?
#echo off
pause
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
net use U: \\abc\def
SET "src="U:\Source Folder""
SET "dst=C:\Destination Folder"
SET "file_list=C:\files.txt"
SET "out=.\errors.log"
echo > %out%
FOR /F "usebackq eol=| delims=" %%f IN ("%file_list%") DO (
rem just searching to find out existense of file
WHERE /Q /R %src% "%%f"
IF "!ERRORLEVEL!" == "0" (
FOR /F "usebackq eol=| delims=" %%s IN (`WHERE /R %src% "%%f"`) DO (
echo "%%s => %dst%\%%f" >> %out%
#copy /y "%%s" "%dst%\%%f"
)
) ELSE (
echo %%f >> %out%
)
)
Replace echo %%f >> %out% with set /p out=%%f
[Edit]
Sorry I misunderstood your question.
Is it because you have two quotation marks in all of the drive path set?
As in replace
SET "src="U:\Source Folder""
With
SET src="U:\Source Folder"
I am writing a script to satisfy following conditions:
I have a csv file with 2 columns, I need to read the file line by line and take values from second column only , lets say values are test1, test2 etc.
Once I have the values, I want to search a windows directory as test1.log etc.
If I get a match against any entry during the search, I want to copy the item to another windows dir.
I have the input file and the search directory is constant as well as file name format is constant.
I used following script:
#ECHO OFF
FOR /f "tokens=2 delims=," %%b IN (test.txt) DO^
( dir *%b%*.log IN "D:\test" if
(*%b%*.log = "" echo "file not found" )
Else DO xcopy "D:\test\"*%b%*.log C:\dest )
But, I can't run the code.
Please help.
Here's an example to help you:
#Echo Off
Set "InFile=test.txt"
Set "ChkDir=D:\test"
Set "SavDir=C:\dest"
If Not Exist "%ChkDir%\" GoTo :EOF
If /I Not "%CD%"=="%SavDir%" (CD /D "%SavDir%" 2>Nul||GoTo :EOF)
For /F "UseBackQ Tokens=2 Delims=," %%A In ("%InFile%"
) Do If Exist "%ChkDir%\*%%~A*.log" (Copy /Y "%ChkDir%\*%%~A*.log">Nul 2>&1
) Else Echo "%ChkDir%\*%%~A*.log not found"
Pause
Still a bit unclear, maybe this works:
#ECHO OFF
FOR /f "tokens=2 delims=," %%b IN (test.txt
) DO If not exist "D:\test\*%%b*.log" (
echo "file "D:\test\*%%b*.log" not found"
) Else (
xcopy "D:\test\*%%b*.log" C:\dest
)
I have the multiple images in sub folder, I don't know how many folders are there. I want to Batch script to find listed names in all the folders and copy the images to destination folder. I tried below script but am getting file not found error.
#echo off
rem Find files and copy files
setlocal EnableExtensions EnableDelayedExpansion
set "SourceBaseFolder=D:\System backup\picture batch file\Test\15oct2015"
set "TargetBaseFolder=C:\OutputFolder"
if not exist "%SourceBaseFolder%\*" (
echo %~nx0: There is no folder %SourceBaseFolder%
set "ErrorCount=1"
goto HaltOnError
)
cd /D "%SourceBaseFolder%"
if not exist "FileNames.txt" (
echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
set "ErrorCount=1"
goto HaltOnError
)
set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
for /R %%J in ("%%N*") do (
set "FilePath=%%~dpJ"
if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
md "!TargetPath!" 2>nul
if exist "!TargetPath!\*" (
echo Copying file %%~fJ
copy /Y "%%~fJ" "!TargetPath!" >nul
) else (
set /A ErrorCount+=1
echo Failed to create directory !TargetPath!
)
)
)
)
:HaltOnError
if %ErrorCount% NEQ 0 (
echo.
pause
)
endlocal
Can any one fix this problem? Thanks in advance.
The solution you're trying to get to work seems excessively convoluted
I believe something similar to the following should do what you want
FOR /F %%G IN ('dir /a-d /s /b "D:\System backup\picture batch file\Test\15oct2015"^|findstr /I /E /G:"D:\System backup\picture batch file\Test\15oct2015\FileNames.txt"') DO (
copy "%%G" "C:\OutputFolder"
)
To ensure the filename matches are exact, all filenames in filenames.txt should be preceded by backslash, e.g.
\filename1.file
\filename2.file
You can easily generate such a file within a batch file, if necessary
I have a section of script that currently runs through a directory of files 'processing' all files specified before deleting them:
for %%x in (*.J_E, *.J_T, *.J_I, *.bcc) do (
"%%x"=="*.exe" (
set /a count=count+1
set choice[!count!]=%%x
)
echo.
::convert files
md %FreshDate%\%FreshTime%
for /l %%x in (1,1,!count!) do (
echo %%x. !choice[%%x]!
tlv2txt !choice[%%x]! > %FreshDate%\%FreshTime%\!choice[%%x]!.txt
del !choice[%%x]!
)
The list of files that need this processing is growing almost weekly and I think it will probably just be easier to process all files except a small number (.dll, *.exe and *.bat)
I have tried substituting this line at the start of the example above:
for %%x in (*.J_E, *.J_T, *.J_I, *.bcc) do (
with this:
for %%x in (*) do if not "%%x=="*.dll" if not "%%x"=="*.bat" if not "%%x"=="*.exe" (
but all I can manage to do with this is delete everything in the directory - including the batch file running the script!
Can anyone help?
Many Thanks
your if construct won't work. Batch's if is very basic.
Use another method: echo the extension of your file and use findstr to check if the string is not (/v) one of the given ones.
for %%A in (*) do (
echo %%~xA|findstr /v /x ".dll .bat .exe" && (
tlv2txt %%A > %FreshDate%\%FreshTime%\%%~nA.txt
)
)
#echo off
setlocal EnableDelayedExpansion
set "exclude=.dll.exe.bat."
for %%x in (*) do if "!exclude:%%~Xx.=!" equ "%exclude%" (
echo %%x
tlv2txt %%x > %FreshDate%\%FreshTime%\%%~Nx.txt
del %%x
)
In this method the extension of the files is compared vs. a list of excluded extensions in a very simple way: the extension is removed from the list, so if the result is the same, such extension is not in the list. This method don't use any external command, like find or findstr, so it run faster.
Wether inclusion nor exclusion need to be that complicated:
:: Inclusion
For /f "Delims=" %%A in (
'Dir /B *.J_E *.J_T *.J_I *.bcc'
) Do tlv2txt "%%A" > "%FreshDate%\%FreshTime%\%%~nA.txt"
:: Exclusion
For /f "Delims=" %%A in (
'Dir /B ^| findstr /i /V ".bat$ .cmd$ .exe$ .dll$" '
) Do tlv2txt "%%A" > "%FreshDate%\%FreshTime%\%%~nA.txt"
I would also remove the unnecessary for loop and just have one:
If Not Exist "%FreshDate%%FreshTime%\" MD "%FreshDate%%FreshTime%"
For /F "Delims=" %%A In ('Dir/B/A-D^|FindStr/VIE "\.dll \.exe \.bat') Do (
tlv2txt "%%A">"%FreshDate%%FreshTime%\%%~nA.txt" 2>Nul
If Not ErrorLevel 1 Del "%%A"))
Please not that I was unable to determine the format of your %FreshTime% variable as it was excluded from your code snippet. Please make sure that it doesn't contain a trailing backspace.
I have created a batch file in a windows server to parse the name of the files stored in a folder.
The name of the file contains a set of parameters splitted by the hyphen, e.g.
ACC-INV-APR-2015
I need to check the syntax correctness of the first two parameters (department and document type) e.g. I would avoid that the hyphen is inserted more than a time in the file name.
On the basis of the check, the files with wrong syntax will be moved to a folder.
We have to consider that, apart the first two parameters that are mandatory, the other ones could be skipped and therefore the file name could have some repetitive hypens after the first two parameters, e.g.
FIN-DOC-APR-2015--MFH-P01
We would avoid to have some file name like: FIN--DOC-APR-2015-MFH-P01
I have created the following batch file but I don't know how to skip the filename with wrong syntax....
Thank you.
setlocal EnableDelayedExpansion
set source=\\fileserver\share$\archive
set dest_ok=\\fileserver\share$\fileok
set dest_not=\\fileserver\share$\error
FOR /R %source% %%f in (*.*) do call :Proc1 "%%f"
goto End
:Proc1
Set filename=%1%
For %%A in (%filename%) do (
Set Folder="%%~dpA"
Set Name=%%~nxA
)
for /f "tokens=1,2 delims=- " %%a in ("%Name%") do call :Proc2 %%a %%b
goto :eof
:Proc2
set department=%1
set typedoc=%2
FINDSTR /x "%department%" c:\0_scripts\arch\departments.txt
if %errorlevel%==0 FINDSTR /x "%typedoc%" c:\0_scripts\arch\typedocs.txt
if %errorlevel%==0 move /Y %filename% %dest_ok%
if %errorlevel%==1 move /Y %filename% %dest_not%
goto :eof
:End
Sounds like a job for regular expressions. The Windows utility findstr will let you match based on a regular expression. It exits status 0 if found, non-zero otherwise. This lends itself to conditional execution. In a cmd console, enter findstr /? for details on supported regexp syntax.
It'll also speed things up to cache the contents of departments.txt and typedocs.txt into variables, rather than open, read, close, repeat for each file checked.
So, with that in mind, here's how I'd do it:
#echo off
setlocal
set "source=\\fileserver\share$\archive"
set "dest_ok=\\fileserver\share$\fileok"
set "dest_not=\\fileserver\share$\error"
set "departments.txt=c:\0_scripts\arch\departments.txt"
set "typedocs.txt=c:\0_scripts\arch\typedocs.txt"
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%I in ("%departments.txt%") do set "dept=%%~I;!dept!"
for /f "usebackq delims=" %%I in ("%typedocs.txt%") do set "type=%%~I;!type!"
endlocal & set "dept=%dept%" & set "type=%type%"
for /r "%source%" %%I in (*) do (
rem // Does filename match /^[a-z]+-[a-z]+/i ?
echo %%~nxI | findstr /i "^[a-z][a-z]*-[a-z][a-z]*" >NUL && (
rem // Yep. Check whether department and doc type is in allowed list.
for /f "tokens=1-2 delims=- " %%a in ("%%~nxI") do (
// if %dept% doesn't equal itself with %%~a removed, and so on...
setlocal enabledelayedexpansion
if not "%dept%"=="!dept:%%~a=!" if not "%type%"=="!type:%%~b=!" (
// All checks passed. Moved to OK folder.
move /y "%%~fI" "%dest_ok%"
)
endlocal
)
)
// If the file hasn't been moved by now, it must've failed a test.
if exist "%%~fI" move /y "%%~fI" "%dest_not%"
)
C:\0_scripts\arch>(
echo MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf | findstr /i "^[a-z][a-z]*-[a-z][a-z]*" 1>N
UL && (for /F "tokens=1-2 delims=- " %a in ("MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf") do (
setlocal enabledelayedexpansion
if not "TEF6;TEF10;QMM8;QMM73;QMM72;QMM71;QMM7;QMM6;QMM13;QMM1;QMM;MFP2;MFP1;MFH3;MFH2;MFH1;MFH ;MFG3;MFG22;MFG21;MFG2;
MFG11;MFG1;MFG;MFB;HSE;COS;" == "!dept:%~a=!" if not "WPL;WP;WBP;WB;WAL;WAG;WA;VTL;VTK;VDP;VBT;VBL;VB;VAW;VAP;VA;UVA;UMV
;TSS;TRN;TKU;TDC;SYM;SWD;SWC;SW;SVS;SVA;SV;STR;STL;STF;STB;SPC;SBT;SAM;RTZ;RTP;RPL;RP;RNO;RHW;RAW;QMP;QMA;QM;QBG;QB;QAM;
PZB;PUM;PRV;PRS;PRJ;PRA;PQP;PPM;PPK;PP;PNR;PLB;PH;PFH;PDV;PDR;PDC;PDB;PAP;PAL;PAG;OPS;OPL;OEE;NOR;NKA;MUB;MSZ;MON;MOD;MB
B;MNT;LZT;LZS;LZN;LPV;LPN;LPL;LPC;LPA;LHT;LDP;LBA;KSB;KPV;KPA;KOE;KOB;KBU;KBL;KB;IAM;HZG;HZ;HSE;HRB;HFG;HF;HE;HAZ;GMD;GE
Z;GBB;FVT;FRM;FPL;FPK;FPI;FPA;FP;FMP;FME;FMD;FMA;FLP;FLB;FIM;FHW;FGY;FGV;FGS;FGP;FGL;FGK;FGE;FGD;FGB;FGA;FDA;FA;EZZ;EWZ;
EWS;EVT;EV;ETZ;ETL;ESZ;EPB;EP;ECM;DVL;ECR;DV;DRX;DRW;DRV;DRQ;DRK;DRF;DMD;DIF;DLP;DER;DDI;DBL;DB;DAT;D01;CPC;CIP;CHL;CE;C
AP;BVT;BVS;BVB;BV;BUG;BSV;BST;BSS;BS;BPZ;BLD;BDL;BBL;BBD;BB;BAL;BAD;ANH;AGZ;AFK;AEN;AED;AAW;AA;" == "!type:%~b=!" (
move /y "\\server1\digit$\deposito\MFH3\MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf" "\\server1\digit$\errori"
pause
)
endlocal
) )
if exist "\\server1\digit$\deposito\MFH3\MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf" move
/y "\\server1\digit$\deposito\MFH3\MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf" "\\server11\digit$\ok"
pause
)
1 file(s) moved.
Press any key to continue . . .
Blockquote
I have changed the script and came back to your original version .
This is the output of the batch file when a file correct is processed:
if exist "\server1\digit$\deposito\MFH3\MFH3--FHW-20150512-F01MD14861-20301231-V02-OP20-TRIFLEX-CP1H--.pdf" move
/y "\server1\digit$\deposito\MFH3\MFH3--FHW-20150512-F01MD14861-20301231-V02-OP20-TRIFLEX-CP1H--.pdf" "\server1\
digit$\errori"
pause
)
1 file(s) moved.
Press any key to continue . . .
C:\0_scripts\arch>(
echo MFH3-AFK-20150511-F01MD12340-20301231-V07-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L;BBD;BB;BAL;BAD;ANH;AGZ;AFK;AEN;AED;AAW;AA;" == "!type:%~b=!" (
move /y "\server1\digit$\deposito\MFH3\MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf" "\bars
rv11\digit$\ok"
pause
)
endlocal
) )
if exist "\server1\digit$\deposito\MFH3\MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf" move
/y "\server1\digit$\deposito\MFH3\MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf" "\server1\
digit$\errori"
pause
)
1 file(s) moved.
Press any key to continue . . .