Reputation: 1759
I am new with .bat script. I am trying to create a .bat file for Windows that will allow me to move only pdf files that contain the substring "A20".
In my example any file is moved, I don't understand what is wrong.
@echo off
echo %1|find ".pdf" >nul
if errorlevel 0 (
echo %1|find "A20" >nul
if errorlevel 1 (echo notfound) else (move C:\Users\Jesus\Downloads\source\%1 C:\Users\Jesus\Downloads\destination_A20\%1)
) else (
echo notfound
)
When I run the script, any file is moved. I need to move only pdf files when file name contains "A20"
How can I do that?
Upvotes: 0
Views: 437
Reputation: 1
You can use findstr instead.
@echo off
echo %1|findstr /i /c:".pdf" >nul
if errorlevel 0 (
echo %1|findstr /i /c:"A20" >nul
if errorlevel 1 (echo notfound) else (move C:\Users\Jesus\Downloads\source\%1 C:\Users\Jesus\Downloads\destination_A20\%1)
) else (
echo notfound
)
Upvotes: 0
Reputation: 14290
A couple of options for you. You can use the command modifiers to isolate the file extension. You can then use string substitution to see if A20 exists within the base file name. You can read about the modifiers by reading the HELP for the CALL
command and you can read about string substitution by reading the help for the SET
command.
set "filename=%~n1"
IF /I "%~x1"==".pdf" (
IF NOT "%filename:A20=%"=="%filename%" (
move "C:\Users\Jesus\Downloads\source\%~nx1" "C:\Users\Jesus\Downloads\destination_A20\"
) else (
echo File is is a PDF but A20 not found
)
) else (
echo file is not a PDF
)
If you just wanted to do a simple move from one directory to another you could just use wildcards.
move "C:\Users\Jesus\Downloads\source\*A20*.pdf" "C:\Users\Jesus\Downloads\destination_A20\"
Upvotes: 2