Batch To Remove Character From A String - batch-file

this may seem basic but Is there a way to create a batch to remove char from a string from a txt file. ?
If I already have this inside the .txt file
2005060.png
2005070.png
2005080.png
2005090.png
so is there a way to create a batch file that will remove the .png at the end to show only this in a new .txt file
2005060
2005070
2005080
2005090
Thanks for any help on this! :)

You can do it as per the following command script:
#setlocal enableextensions enabledelayedexpansion
#echo off
set variable=2005060.png
echo !variable!
if "x!variable:~-4!"=="x.png" (
set variable=!variable:~0,-4!
)
echo !variable!
endlocal
This outputs:
2005060.png
2005060
The magic line, of course, is:
set variable=!variable:~0,-4!
which removes the last four characters.
If you have a file testprog.in with lines it it, like:
2005060.png
1 2 3 4 5 leave this line alone.
2005070.png
2005080.png
2005090.png
you can use a slight modification:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "delims=" %%a in (testprog.in) do (
set variable=%%a
if "x!variable:~-4!"=="x.png" (
set variable=!variable:~0,-4!
)
echo.!variable!
)
endlocal
which outputs:
2005060
1 2 3 4 5 leave this line alone.
2005070
2005080
2005090
Just keep in mind that it won't output empty lines (though it will do lines with spaces on them).
That may not be a problem depending on what's allowed in your input file. If it is a problem, my advice is to get your hands on either CygWin or GnuWin32 (or Powershell if you have it on your platform) and use some real scripting languages.

This worked best for me:
#echo off
for %%x in (*.png) do echo %%~nx
credit

If your trying to read a directory of .png files and output a list without extensions?
Try this:
#echo off
echo. > testprog.txt
for /R "C:\Users\%USERNAME%\Documents" %%f in (*.png) do (
echo %%~nf >> testprog.txt
)
start testprog.txt

Related

Combine lines in text file using batch

I want to make a program that takes the content of the second line of a text file and puts it on the first. (It doesn't matter if the second doesn't get edited)
for /f "tokens=1" %%t in (file.txt) do set string1=%%t
for /f "tokens=2" %%t in (file.txt) do set string2=%%t
echo %string1%%string2%>file.txt
I have two issues hat I can't seem to be able to fix.
One: the loops only include the first word of each line in the variables.
Two: Echo doesn't replace the first line of the file with the variables given and instead writes ECHO command deactivated (I have the French version of Windows 10 and simply translated what got written in the file, the text in English Windows version might be slightly different, but you get the idea)
If you have any suggestions, I would appreciate if you explain what the code you provide does (I always like to learn)
Your question is not clear and can be understood in several different ways. Anyway, this management is simpler with no for command:
#echo off
setlocal EnableDelayedExpansion
< file.txt (
rem Takes the content of the first line
set /P "line1="
rem Takes the content of the second line and puts it on the first
set /P "line2="
echo !line1!!line2!
rem It doesn't matter if the second line doesn't get edited
echo !line2!
rem Copy the rest of lines
findstr "^"
) > output.txt
move /Y output.txt file.txt
The FOR command uses a space as a delimiter by default. So you have to tell it to not use any delimiters with the DELIMS option. Also, you should be able to do this with a single FOR /F command. Just hold the previous line in a variable.
#ECHO OFF
setlocal enabledelayedexpansion
set "line1="
(for /f "delims=" %%G in (file.txt) do (
IF NOT DEFINED line1 (
set "line1=%%G"
) else (
echo !line1!%%G
set "line1="
)
)
REM If there are an odd amount of lines, line1 will still be defined.
IF DEFINED line1 echo !line1!
)>File2.txt
EDIT: I think I completely misunderstood your question. Once you clarify your question I will repost a code solution if needed.
Use skip to omit the first line and write the 2nd line twice. In general an edit of a file implies a rewrite to a new file and possibly a rename to retain the old file name.
:: Q:\Test\2018\07\25\SO_51508268.cmd
#Echo off
Set "flag="
( for /f "usebackq skip=1 delims=" %%A in ("file1.txt") Do (
If not defined flag (
Echo=%%A
Set flag=true
)
Echo=%%A
)
) >file2.txt
Del file1.txt
Ren file2.txt file1.txt
After running the batch a file1.txt with initially numbered lines 1..5 looks like this:
> type file1.txt
2
2
3
4
5

Looking for a script to rename multiple files with charactares already in filename

I have many files in many folders that I need to rename.
And example is
from cgs2016-09-05-05-40-34.xls
to cgs0905.xls
and
from cgs2016-09-06-05-40-34
to cgs0906.xls
etc
Any help would be greatly appreciated!
#Jamaz Try out the following code below on a sample of your files. Again please use it on a test sample of your files so it does not cause you issues if it makes a mistake. Thank you and please up-vote if this works for you.
setlocal enabledelayedexpansion
REM Collect list of file names in current directory that have the .xls file extension. Output to text file.
dir /b "*.xls" >xls.txt
REM For loop examines the text file for individual file names.
FOR /F "tokens=1" %%# in (.\xls.txt) do (
REM SET variable "#" to equal "token"
Set "token=%%#"
REM Extract the first 3 characters (year) from the file name and set is to variable "token"
Set "tokenchar=!token:~0,3!"
REM Extract the month characters from the file name and set the variable as "tokenmonth"
Set "tokenmonth=!token:~8,2!"
REM Extract the day characters from the file name and set the variable as "tokenday"
Set "tokenday=!token:~11,2!"
ren "%%#" "!tokenchar!!tokenmonth!!tokenday!.xls"
echo %%#
)
Pause
not the best way, but works for your examples:
#echo off
setlocal enabledelayedexpansion
for %%x in (*.xls) do (
set "filename=%%x"
ECHO ren "%%x" "!filename:~0,3!!filename:~8,2!!filename:~11,2!.xls"
)
remove the ECHO if output is ok.
Because the last nineteen characters, date and time stamp, are more likely to be constant than the first three, (especially over multiple folders), I'd change both the previous answers to cater for that rationale.
#Echo Off
SetLocal EnableDelayedExpansion
(Set _rf=C:\Users\jamaz\TestDir)
(Set _fe=xls)
If Not Exist "%_rf%\" Exit/B
For /R "%_rf%" %%I In (*.%_fe%) Do (Set "_fn=%%~nI"
Echo=Ren "%%I" "!_fn:~,-19!!_fn:~-14,2!!_fn:~-11,2!%%~xI")
Timeout -1 1>Nul
EndLocal
Exit/B
As the OP was not clear about whether the code was for multiple same level folders or subfolders rooted from a single location, I went for the latter as the previous responses had already covered that.
Change your chosen file path and extension on lines 4 and 5
If you are happy with the console output, remove echo= from line 10 and delete line 11

Batch - Looping in 'FileNames_Constant.Ext' , and Print 'FileNames' as an Output

I've got some *.Xml files in a directory and its sub-directories. I need to loop through the XML files which have a specific constant at the end of their file name, and then echo/print their names without the constant part nor the extension (.Xml).
For example: these are the file names I have:
FileAAA_Constant.Xml
FileBBB.Xml
FileCCC.Xml
FileDDD_Constant.Xml
And this is the output I need:
FileAAA
FileDDD
I've tried this command:
For /R %%X in (*_Constant.Xml) do echo %%~nX
Which outputs this:
FileAAA_Constant
FileDDD_Constant
As you can see, it has removed the extension only, while I need to remove "_Constant.Xml" as well.
This works if the file names contains only one underscore, as indicated in your example:
for /F "delims=_" %%X in ('dir /S /B *_Constant.Xml') do echo %%X
If the desired file names may contain more than one underscore, use Pokechu22's answer.
Fairly easy if the exact length of the phrase is known; you just need to use the %var:~0,-3% syntax. Since "_Constant" is 9 chars long, you would want %var:~0,-9%, which takes text from the start (0) to 9 chars from the end (-9). Aditionally, delayed variable expansion also must be enabled with setlocal ENABLEDELAYEDEXPANSION for this to be run inside of your For loop.
Here's a full example:
#Echo off
setlocal ENABLEDELAYEDEXPANSION
for /R %%X in (*_Constant.Xml) do (
set FileNameTemp=%%~nX
echo !FileNameTemp:~0,-9!
)
Note that if you have a file named just _Constant.xml, this will produce "ECHO is off." rather than "" (no output). This can be solved by changing echo !FileNameTemp:~0,-9! to echo. !FileNameTemp:~0,-9!, but that puts a space before each output.
Here's the loop alone:
for /R %%X in (*_Constant.Xml) do (
set FileNameTemp=%%~nX
echo !FileNameTemp:~0,-9!
)

merge lines from several files

I have 3 text files like this
file1.txt
AAA1
AAA2
AAA3
...
...
file2.txt
BBB1
BBB2
BBB3
..
..
file3.txt
CCC1
CCC2
CCC3
..
..
I want to have output.txt like this
AAA1:BBB1:CCC1
AAA2:BBB2:CCC2
..
...
now I can do this by making a loop reading a line from each file but I want to do this using sed , grep or any similar tools but I dont know how.
thanks
Use paste:
paste -d ':' file1.txt file2.txt file3.txt
paste is well suited to this task. Try:
paste -d ":" file1.txt file2.txt file3.txt > output.txt
The paste answer sounds like a perfect solution.
But someone (not the OP) might want a native solution that does not require downloading an executable. Something like this really should be done with a more powerful scripting language like VBScript or JScrpt.
But, since I like the challenge of solving things with batch, I thought I'd take a stab at a native batch solution, just for fun.
The OP said "now I can do this by making a loop reading a line from each file". That is easier said than done using batch!
Normally files are read using FOR /F, but there is no good way to interleave reads from multiple files using FOR /F. The only other native alternative is to use SET /P with redirected input. This technique imposes the following limitations:
The input files must use Windows style line terminators: <carriage return><line feed>
No input line can exceed 1021 bytes in length (disregarding the line terminators)
Trailing control characters are stripped from each input line
In addition, each final merged line must not exceed the batch variable length limit of ~8k bytes.
One last restriction - the script can only handle up to 7 input files. The script will fail if more than 7 files are specified - I did not include any error checking.
So here is a working batch script - call it "merge.bat". Note - this batch solution is MUCH slower than other solutions like paste or scripts written in VBScript or JScript. But it does work :-)
#echo off
setlocal disableDelayedExpansion
::Initialization
set "inputRedirection="
set "files=2"
set "lines=0"
for %%F in (%*) do call :setInputRedirection
setlocal enableDelayedExpansion
set "inputRedirection=!inputRedirection:::=<!"
::Merge the files
%inputRedirection% (
for /l %%N in (1 1 %lines%) do (
set "ln="
for /l %%I in (3 1 %files%) do (
call :readLine %%I
if %%I neq %files% (
set "ln=!ln!!input!:"
) else (
echo(!ln!!input!
)
)
)
)
exit /b
:setInputRedirection
set /a "files+=1"
for %%A in (1) do (
set inputRedirection=%files%::"%%~F" %inputRedirection%
for /f %%N in ('find /c /v "" ^<"%%~F"') do if %%N gtr %lines% set "lines=%%N"
)
exit /b
:readLine fileHandle
set "input="
<&%1 set /p "input="
exit /b
To merge your files you would do:
merge.bat file1.txt file2.txt file3.txt >output.txt

Batch file to rename files

We have a couple of thousand files in a directory named like this:
EXP_10000021.XM_
And need to remove the leading 1 so the new file name is:
EXP_0000021.XM_
I'm no good with batch files - any help would be appreciated!
If your filenames start all with EXP_1 it's easy.
setlocal EnableDelayedExpansion
for %%A in (EXP_1*.XM_) do (
set "filename=%%A"
set "newName=EXP_!filename:~5!"
rem ** remove the ECHO when it seems to work
ECHO ren !filename! !newName!
)

Resources