I am new to Win10 batch files and I am running into a problem. I want to parse a directory for files that meet a certain criteria and then return a substring of the file.
Example:
If the file name is 210526_FPRS.PAS.SKBTXNS.TXT -> I want to echo 210526_FPRS.PAS.SKBTXNS
If the file name is 210526_FPRS.PAS.XXXXXXX.TXT-> I want to take no action
Here is what I wrote:
#ECHO OFF
ECHO "Looping through files..."
CD \blah
FOR %%G in ("*SKBTXNS*") DO ECHO %%G:~1,22%
The output I am getting is: 210526_FPRS.PAS.SKBTXNS.txt:~1,22
Thanks in advance!
#ECHO OFF
CLS
ECHO Looping through files...
CD \blah
FOR %%G IN (\*SKB\*) DO (call :uFile: %%G)
GOTO :eof
:uFile
SET _filename=%1
SET _result=%_filename:~0,22%
ECHO %_result%
:eof
The issue that I found in my original code was that I was trying to update my variables inside the FOR loop. What I needed to do was set them outside the loop via a subroutine. From there I was able to adjust the substring value as needed for the situation.
Thanks to all for your help!
Related
I'm new to batch scripting and need help here.
My file name along with path is
C:\test\My_Test_File_20201006.txt
and I want to rename it as
C:\test\My_File_20201006.txt
using batch script only. I cannot use PowerShell here.
#echo off
set Pattern="Test_File"
set Replace="File"
Rem accepts the filename as cmd line argument
set filename=%1
Rem Update filename
set targetfile=%filename:Pattern=Replace%
Rem Rename the file
Ren %filename% %targetfile%
Exit
Using the above code, My file is renamed as "File". Tried % around the Pattern & replace variables, but no luck. Not sure where I'm going wrong. Tried all possible solutions from the StackOverflow and other tutorials, but none helped.
Edit:
After the proposed solution, getting a syntax error. The code is as below:
#echo off
set "filename=%~nx1"
echo %filename%
echo "%~dp1"
echo "%~dp1%filename:statement_=%"
ren "%~dp1%filename%" "%~dp1%filename:Test_=%"
I call my script from cmd line as:
D:/Test> C:/script/rename.bat C:\test\My_Test_File_20201006.txt
The echo statement correctly prints filename, directory & filename with the directory. Facing issues in rename statement.
Output:
My_Test_File_20201006.txt
"C:\test\"
"C:\test\My_Test_File_20201006.txt"
The syntax of the command is incorrect.
Three things wrong here.
You cannot add the quotes as part of the variable's value. It will actually use them as part of the variable. set variables to have double quotes including the variable name. For instance instead of set Pattern="Test_File" rather do set "Pattern=Test_File"
You never used the variables you've set Replace and Pattern
You either need to enabledelayedexpansion or use call to do the replacement because of the multple % required.
#echo off
set "Pattern=Test_File"
set "Replace=File"
Rem accepts the filename as cmd line argument
set "filename=%~nx1"
Rem Update filename
setlocal enabledelayedexpansion
ren "%~dp1%filename%" "!filename:%Pattern%=%Replace%!"
Another method, seeing as you only replace Test_ in your example:
#echo off
set "filename=%~nx1"
ren "%~dp1%filename%" "%filename:Test_=%"
EDIT
Fixing your example as per the edit.
#echo off
set "filename=%~nx1"
echo %filename%
echo "%~dp1"
echo "%~dp1%filename:statement_=%"
ren "%~dp1%filename%" "%filename:Test_=%"
I am trying to read in a directory and get the filename from that directory.
I then want to save the filename as a variable and echo that filename out.
Here is the code I am using:
for /F %%a in ('dir C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\ *.csv') do set FileName=%%a
echo %FileName%
I am getting the following when command prompt runs:
"File not found
Directory"
Does anyone know how to resolve this or where I'm going wrong?
Thanks
Safer way of doing the same:
#echo off
setlocal
set "yourDir=C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\"
set "yourExt=*.csv"
pushd %yourDir%
for %%a in (%yourExt%) do echo %%a
popd
endlocal
Sets both: Your directory and the extension you are searching for, Changes the directory to the one previously setted possibly including a /drive change and then runs a loop over all files matching your extension and echo them out. To save only the last one you can use:
...do set fileName=%%a
echo %FileName%
Or to use them all within the loop you can use:
#echo off
Setlocal EnableDelayedExpansion
REM Other things done here
do (
REM Do stuff with %%a here
Set filename=%%a
echo !filename!
echo !filename:~0,6!
echo !filename:a=b!
)
If you just want to echo them, you can just go for echo %%a. If you want to do other things like string-substitution or substrings as described in the comments you need DelayedExpansion as shown above. There are a lot of questions on SO as well.
Note that you can get different "parts" of the path of your file. Have a look on this answer I always have a look on as well. Alternatively check the documentation for the for command typing for /? into the command-line.
I am trying to get this script to jump to another section of the script if there is no input from the user.
Down at the if %input%== area.
What I'm trying to do is skip to the section where the script checks for .mp4 files and moves them if they are there. Am I supposed to set a variable or loop for that section? Thanks for any replies
#echo off
echo Checking for youtube-dl updates.
pause
youtube-dl -U
rem Enter the url or urls that you want to download from
set /p input="Enter the url(s) you want to download:"
rem Uses the youtube-dl continue (-c) option to download multiple files if not in a playlist
youtube-dl -c "%input%"
rem pause
if %input%=="" GOTO:EOF
cls
echo Download complete, please wait while files are transfered to appropiate folder
pause
for %%o in (.mp4) do move "*%%o" "E:\Documents\scripts\videos\"
if not exist do echo .mp4 files are no longer in this directory
pause
How about following script? This script waits for 3 seconds while "file.mp4" doesn't exist. It keeps to wait until the file exists. About "filename", you can change for your script.
#echo off
set waittime=3
set filename=file.mp4
:loop
if not exist %filename% (
Timeout /t %waittime% /nobreak > nul
goto loop
)
echo Find %filename%
When doing string comparison in batch you have to make sure, that both parts are equal, which in your case will never happen! In most languages strings have double quotes around them. In batch they usually do not.
To solve your problem enclose %input% in double quotes as well.
Note that it can be useful to do something like "x%input%"=="x" to prevent certain characters like <>|to be at the beginning of the comparison string.
You can check this on your own with these few lines:
#echo off
set /p input="Input something or nothing here "
echo %input%
echo "%input%"
pause
If you are hitting Return without any input you will see that only the bottom one will output "" which is the string you are comparing to.
I have a question. Is it possibile in batch language to search in folder a part of name that is same like another file and display it.For example i got folder with files :
ggggggsss.mp3
ddddddeee.mp3
ddddddff.mp3
ssssssddd.mp3
aaaaasssss.mp3
11111ssdas.mp3
11111dddd.mp3
...
I need to display in cmd only names of files
ddddddeee
ddddddff
and
11111ssdas
11111dddddd
Because the first six letter are the same. Could someone help me with this problem?
Save this script to test.bat and run from open Cmd Prompt. Replace dir value with path to your folder with .mp3 files:
#echo off
setlocal enabledelayedexpansion
set "dir=%userprofile%\music"
set "pattern1=dddddd" & set "pattern2=11111"
pushd "%dir%"
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,6!"=="%pattern1%" echo %%G)
echo/
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,5!"=="%pattern2%" echo %%G)
popd
exit /b
See also Extract Substrings.
I'm fairly new to batch scripting, and I need to write a pretty simple .bat file that will loop through a directory. I know how to do it pretty easily using the goto command:
#echo off
:BEGIN
::set variable to data in first file
::do operations on file...
IF ::another file exists in the directory
::increment to next file
GOTO BEGIN
ELSE
GOTO END
:END
cls
The problem is that's the only way I can think of to do it. I know goto's are generally very frowned upon to use, so I was wondering if anyone knows another way to do this? Thanks!
Replace the echo.... with your desired command.
From the Command prompt:
for /R %A in (*.*) do echo.%A
In a bat file
for /R %%A in (*.*) do echo.%%A
It can be done just for the current folder too. The %%a metavariable is case sensitive and I choose to use lower case. The script will exit and quit when all files are processed.
#echo off
for %%a in (*.txt) do (
type "%%a"
pause
)