i have a problem, i need a code as in title. purpose of the script: i need to copy the file with a different language code, f.e. XXX_x_xYYY_EN_xx to XXX_x_xYYY_ES_xx and XXX_x_xYYY_DE_xx. So far i have this, but it does not work:
setlocal enableDelayedExpansion
for /l %%H in (1,1,2) do (
set endf[%%H]=!fName[%%H]:*_EN_=!
set trim[%%H]=_EN_!endf[%%H]!
set beginf[%%H]=!fName[%%H]:%trim[%%H]%=!
)
fName is set elswere, its something like this
fName[1]=XXX_x_xYYY_EN_xx
fName[2]=XXXXX_x_xYYYY_EN_x
Everything works except the set beginf(it spits out "beginf[1]=fName[1]:="), i've tryed myriads of % nad ! combos. Fun fuct it works in this case (other script, same puprose but work for only one file in directory, i'd like to make it more versatile):
set beginf=!NAME:%langcode%=!
help :) thanks!
So you have two diferent conversions for two array elements, isn't it? So you just need to match each filename with its corresponding conversion, right?
Something like this, perhaps?
set "conv[1]=ES"
set "conv[2]=DE"
for /l %%H in (1,1,2) do (
for %%c in (!conv[%%H]!) do set "newName[%%H]=!fName[%%H]:_EN_=_%%c_!"
)
I suggest you to read this answer.
EDIT: New method added
This code do exactly the same than the one in your posted answer:
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.xml) do (
set "fName=%%a"
for %%c in (ES DE PL) do (
copy "%%a" "!fName:_EN_=_%%c_!"
)
)
A comparison of your code vs. this one:
You really not need an array of file names. If you create the array just to process its elements once with no further processing, then you are wasting the space occupied by the array. You may do the same thing using an individual file name variable.
Also, you not need an array of conversions. If you just want to repeat a command with several conversions, then it is much simpler to use a list of conversions instead.
It is a bad idea to use special characters as FOR replaceable parameters (where the documentation specifies a letter). Batch files are intrinsically cryptic, so there is no need to include additional complexities...
If you just want to process all files (no rename they) then it is simpler to use a plain for command instead of a for /F one on a 'dir /B' command. The second form requires to execute an additional copy of cmd.exe program and to create a temporary disk file...
If the newfName (array) variable is used just to execute the copy command in the next line, then such a variable is (again) a waste of space. You may create the new name in the copy command itself.
I found the solution :)
Full script with file counting below:
#echo off
setlocal enableDelayedExpansion
set /a count=0
for /f %%# in ('dir *.xml /b') do (
set /a count+=1
set fName[!count!]=%%~xn#
)
set "conv[1]=ES"
set "conv[2]=DE"
set "conv[3]=PL"
for /l %%H in (1,1,!count!) do (
for /l %%G in (1,1,3) do (
for %%c in (!conv[%%G]!) do (
set "newfName[%%G]=!fName[%%H]:_EN_=_%%c_!"
copy !fName[%%H]! !newfName[%%G]!
)
)
)
loops clarification:
for /l %%H - loops through the fName array
for /l %%G - loops through the lang table (conv) array
for %%c - combine above loops and copy files with changed names
thanks #Aacini for your input, it put me on the right tracks :)
Related
I wrote the following batch file:
setlocal EnableDelayedExpansion
set integer=0
for /f "delims=" %%a in ('where /R C:\ /F python.exe') do (
set Paths[!integer!]=%%a
set /A integer+=1
)
for %%b in (%Paths%) do (
echo %%b
)
The idea is that I want to run where /R C:\ /F python.exe and then store the several paths outputted into an array, I need this because I need to handle these paths later on. So far for testing purposes I called the second half of the script just to make sure the array is working properly.
Your second part is wrong. Since there are no real arrays in batch scripting, you cannot use a for loop similar to something like for each element in array do as used in other languages. Refer to this post for details: Arrays, linked lists and other data structures in cmd.exe (batch) script.
To loop through all (pseudo-)array elements you could do the following instead:
set /A count=integer-1
for /L %%i in (0,1,%count%) do echo Paths[%%i] = !Paths[%%i]!
As suggested by Aacini in a comment, you might change your base-0 to a base-1 (pseudo-)array by moving the line set /A integer+=1 upwards to be before line set Paths[!integer!]=%%a in your first part, so the variable integer already contains the greatest index, which would lead to a slightly simpler second part:
for /L %%i in (1,1,%integer%) do echo Paths[%%i] = !Paths[%%i]!
Alternatively, you could use a for /F loop that parses the output of set Paths[:
for /F "tokens=1* delims==" %%i in ('set Paths[ 2^> nul') do echo %%i = %%j
This might be beneficial:
you do not need to know the total number of elements;
the indexes do not have to be adjacent, they could even be non-numeric;
But there are two caveats:
the (pseudo-)array elements are not returned in ascending order with respect to the indexes, because set Paths[ returns them alphabetically sorted, where the order is 0, 1, 10, 11,..., 2, etc., so the order becomes jumbled as soon as there are indexes with more than a single digit;
in case there exist other variables beginning with that name, like Paths[invalid] or Paths[none, they become returned too;
I am trying to create a batch file that can look through a text file.
In the text file, there are rows of texts. I need to go through each row and if it contains the characters \\, then I want to echo that whole row.
I currently have the following, but it does not work. What happens is all the rows end up being echoed, instead of those that have \\.
for /f "delims=*" %%A in (list.txt) do (
if NOT "%%A"=="%%A:\\=%" (
echo %%A
)
)
Anyone able to advise what happens?
MC ND's suggestion of using find is for sure the easiest solution for your task. Anyway, I want to focus on your approach.
You cannot use sub-string replacement with for variable references. You need to store %%A in a normal environment variable and apply sub-string replacement there. However, since you are within a block of code (for loop), you must use delayed expansion. This is how it works:
setlocal EnableDelayedExpansion
for /F "delims=*" %%A in (list.txt) do (
set "TEST=%%A"
if NOT "!TEST!"=="!TEST:\\=!" (
echo/!TEST!
)
)
endlocal
i have an array and a variable
SET users=nick_derus peter_parker john_simpsons cool_guy
set mid=freak
i need to create files named like the following:
"derus freak nick"
"parker freak peter"
"simpsons freak john"
"freak guy cool"
and i'm failing over and over again. any help on how to do it?
As they are unlikely file names, you'll have to determine the command you want to run yourself and change line five accordingly:
#Echo Off
Set users=nick_derus peter_parker john_simpsons cool_guy
Set mid=freak
For %%a In (%users%) Do For /F "Tokens=1* Delims=_" %%b In ("%%a") Do (
Echo createfile "%%c %mid% %%b")
pause
This ignores the fact your last example name doesn't follow the format of the others.
I have several files in a folder with names like
"prefix (S-N 12RE3123) suffix.pdf"
I would like to rename them to just "12RE3123.pdf", removing the prefix and suffix. The issue is that both the prefix and suffix are of varying lengths/ characters, so I cannot just rename by removing the first/last xx characters, but have to use the only commonality where only the characters inside the parenthesis following "S-N" are those to be kept.
There is one special case where a few of the serial numbers are named as WD-12RE3123, and I need to remove the WD- as well. If I had to do it manually, there aren't a lot of them like that so it wouldn't be the end of the world, but having it automated would be nice. I thought of maybe doing an if statement after the removal of prefix/suffix to check if the first 3 characters match WD- then remove those if true, but I am not sure on the implementation.
I am still a novice in batch or vbscript, so I was wondering if this can be done in either of those. Batch has the method "REN" to rename files, but since the final name depends upon what the current name is I am not sure how to set up delimiters or how to approach this.
Any assistance would be greatly appreciated.
Here is a simple solution for your request. It relies on the following facts:
the prefix portion does not contain any parenthesis ( or ) on its own (suffix might though);
the serial number does not contain any parentheses ( or ) on its own;
there are no duplicate serial numbers, also with respect to removal of potentional WD- strings;
This is the code (after having tested the code on the files in your target folder, you need to remove the upper-case ECHO command to actually rename the files):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Set constants here:
set "LOCATION=.\test"
set "PATTERN=*.pdf"
set STRINGS="S-N " "WD-"
cd /D "%LOCATION%"
for /F "eol=| delims=" %%K in ('dir /B "%PATTERN%"') do (
for /F "eol=| tokens=2 delims=()" %%L in ("%%~nK") do (
set "NAME=%%L"
setlocal EnableDelayedExpansion
for %%J in (%STRINGS%) do (
if "!NAME!"=="%%~J!NAME:*%%~J=!" (
set "NAME=!NAME:*%%~J=!"
)
)
ECHO ren "%%~fK" "!NAME!%%~xK"
endlocal
)
)
endlocal
exit /B
How it works:
the first section specifies the folder location, the file pattern and the strings to remove (after having extracted the portion within ()); adapt the values to your needs;
there are several nested for loops; the outermost one simply iterates all the files matching the given pattern (it basically reads and parses the output of a dir command applied on the given files and loops through the returned items; opposed to a standard for loop, this for /F method ensures that the entire folder is read before the loop starts iterating, which is necessary when modifying the enumerated folder content like we do here by renaming files; see also this thread about that issue);
the next for /F loop extracts the file name portion of interest, that is everything between the first pair of parenthesis, and stores it in variable NAME; this loop iterates once only per file;
the is another for loop which walks though all items in the STRINGS variable;
the if clause checks whether the current item of STRINGS occurs at the very beginning of the NAME value; if so, it is removed, otherwise not; this is just a safety query because perhaps a serial number might also contain a given STRINGS item in the middle or at the end (for instance, 123-WD-45A);
at this point, the renaming is performed (after having removed ECHO, of course);
the toggling of delayed expansion is intended to avoid trouble with some special characters in the file names;
And here is another script that uses a more complex method for extracting the interesting parts of the file names. It relies on the following facts:
there occurs only a single substring (S-NSPACE in the file name
the serial number is followed by a );
the serial number does not contain any parentheses ( or ) on its own;
there are no duplicate serial numbers, also with respect to removal of potentional WD- strings;
The code looks like this:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Set constants here:
set "LOCATION=.\test"
set "PATTERN=*.pdf"
set "FILTER=(S-N [^()][^()]*)"
set "IDENTIFYER=S-N "
set STRINGS="WD-"
cd /D "%LOCATION%"
for /F "eol=| delims=" %%K in ('
dir /B "%PATTERN%" ^| findstr /I /R /C:"%FILTER%"
') do (
set "NAME=%%K"
setlocal EnableDelayedExpansion
set "NAME=!NAME:*(%IDENTIFYER%=!"
for /F "eol=| tokens=1 delims=)" %%L in ("!NAME!") do (
setlocal DisableDelayedExpansion
set "NAME=%%L"
setlocal EnableDelayedExpansion
for %%J in (%STRINGS%) do (
if "!NAME!"=="%%~J!NAME:*%%~J=!" (
set "NAME=!NAME:*%%~J=!"
)
)
ECHO ren "%%~fK" "!NAME!%%~xK"
endlocal
endlocal
)
endlocal
)
endlocal
exit /B
Basically, this script works similar to the above one, with a few deviations:
the first section specifies a file name filter and the serial number identifyer (S-N) in addition;
the dir command in the outermost for loop is piped into a findstr command to filter out files not containing (S-N and ) and a string (not containing (/)) in between already at the beginning;
the part (S-N and everything before is removed, the result is stored in NAME;
the next for /F loop extracts everything up to but not including the first ) from the NAME value, which constitutes the file name portion of interest; this loop iterates once only per file;
the is another for loop which walks though all items in the STRINGS variable, which does not contain the S-N portion here as this has already been removed before; the rest in this loop is the same as in the above script;
at this point, the renaming is performed (after having removed ECHO, of course);
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*(*)*.*" '
) DO (
FOR /f "tokens=1,2delims=()" %%c IN ("%%a") DO (
FOR /f "tokens=1-3delims=- " %%m IN ("%%d") DO (
IF "%%o"=="" (ECHO(REN "%sourcedir%\%%a" "%%n%%~xa"
) ELSE (
ECHO(REN "%sourcedir%\%%a" "%%o%%~xa"
)
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Apply each filename matching the mask "()." to %%a
Tokenise using ( and ) as delimiters so that the parenthesised portion is in %%d
Re-tokenise using - and as delimiters so that the required number is in %%o for S-N patterns and %%n for WD- pattern.
Show the rename line.
Note: this should work provided the prefix portion does not contain parentheses and the parenthesised portion is exactly as described.
Magoo and aschipfl both provided good pure batch solutions.
I find development of custom batch scripts for every complex renaming task to be tedious, which is why I wrote JREN.BAT - a regex find/replace renaming utility. JREN.BAT is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. Full documentation is available from the command line via jren /?, or use jren /?? for paged help.
With JREN.BAT, the solution is as simple as (untested):
jren "^.*\((?:S-N (?:WD-)?)(.+?)\).*" "$1.jpg" /fm *.jpg
If I got the regex wrong, it can easily be fixed. You should probably add the /T option to run the command in test mode first, and then remove it when everything looks good.
If you put the command in a batch script, then you must use CALL JREN.
I have a file C:\parameters.txt that contains different parameters, for example:
env_user=username123
env_pw=password123
env_url=example.com
Now I created a .cmd file that needs to get these values and put them in a variable, for example:
SET var_user=<Here I need 'username123'>
SET var_pw=<Here I need 'password123'>
SET var_url=<Here I need 'example.com'>
How do I write this in my cmd script to get the correct values for my variables?
You need to set a delimiter for = character so that words before/after = will be separated. Besides that, you need an array to set each of the parameters. You can do it like this:
#echo off
setlocal enabledelayedexpansion
set increment=0
for /f "tokens=1* delims==" %%a in (C:\parameters.txt) do (
set parameters_array[!increment!]=%%b
set /a increment+=1
)
echo %parameters_array[0]%
echo %parameters_array[1]%
echo %parameters_array[2]%
pause >nul
Keep in mind, array always starts from 0. You could change to set increment=1 if you prefer the array starts from 1.
Just a slight alternative to dark fang's solution, since your parameters.txt file's contents are already in the format of variable=value, you could
#echo off
setlocal
for /f "usebackq delims=" %%I in ("c:\parameters.txt") do set "%%I"
rem // display env_* variables
set env_
pause
The usebackq option allows you to quote the file name, which might be needed if you ever move c:\parameters.txt to a location containing spaces, ampersands, or other tricksy characters. It's a good habit to follow when reading the contents of text files with for /f.
Also, it's better not to use delayed expansion if you don't need it, as delayed expansion can sometimes corrupt values containing exclamation marks -- a situation that is reasonably possible when dealing with passwords.
I've found the solution thanks to different inputs.
#echo off
For /F "tokens=1* delims==" %%A IN (C:\parameters.txt) DO (
IF "%%A"=="env_user" set var_user=%%B
IF "%%A"=="env_pw" set var_pw=%%B
IF "%%A"=="env_url" set var_url=%%B
)
This will set the correct variables (not local) once the specific code name (before the = in parameters.txt) has been found.