Renaming txt files with numbers as filenames in cmd - linux

i have files and This names the files example :
log(2).txt
log(3).txt
log(4).txt
log.txt
I want Rename all of them to file names with numbers like:
1.txt
2.txt
3.txt
4.txt
system windows or linux

Just save this code into a name.bat file in your folder and run the command
#echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.txt) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.txt
Here in order to avoid conflicts first the files are named as '.new' extenstion and later it is converted back to '.txt' files.

Related

Bat file to list files, using semicolon delimiter?

So far I have this:
#ECHO OFF
dir *.txt /c /b /on > content.txt
Which gives output:
file1.txt
file2.txt
file3.txt
But I need it like this, separated with semicolon on each line:
file1.txt;
file2.txt;
file3.txt;
I assume I probably need to write for loop and add string ";" somewhere, but I don't know where or how to do this. Or is there a way to just set a specific delimiter?
Edit:
My usecase changed, I thought it would be better if there are files in subfolders listed as well, but "/" should be replaced with space " ".
Example output:
file1.txt;
file2.txt;
subfolder1 file1.txt;
subfolder2 file1.txt;
Note that I do not want the full parent path, only subfolders.
Quick single line batch-file answer:
#(For /F Tokens^=*^ Delims^=^ EOL^= %%G In ('Dir "*.txt" /A:-D /B /O:N 2^>NUL') Do #Echo %%G;) 1>"content.log"
…and in cmd:
(For /F Tokens^=*^ Delims^=^ EOL^= %G In ('Dir "*.txt" /A:-D /B /O:N 2^>NUL') Do #Echo %G;) 1>"content.log"
I have decided to output to a .log file, so that the listing doesn't include itself.
Please use the built-in help to learn how each command works.
When you read the help information, please be aware that a 'simple' for loop will not pick up all files, it will ignore all hidden files for instance. Also despite any first impressions you may have from testing, the order of files returned, depends upon both the file system and type. The dir command is the most efficient way of ensuring that sort order.
[EDIT /]
Here is a batch-file solution, (as that's what you posted as an answer), for your New and completely different question.
#(For /F Tokens^=*^ Delims^=^ EOL^= %%G In ('Dir "*.txt" /A:-D /B /O:N /S 2^>NUL') Do #(Set "FileName=%%~dpG" & SetLocal EnableDelayedExpansion & Set "Filename=!FileName:~,-1!" & For %%H In ("!FileName:%__CD__%=!") Do #EndLocal & Echo %%~H %%~nxG;)) 1>"content.log"
In future, when you have existing answers to your asked question, do not change that question when not only the main command is different, but the intended result format too.
a Simple for loop will do:
#(for %%i in (*.txt) do #echo %%i;)>"content.txt"
That will however also echo the content of contents.txt to itself if it exists, so you can exclude it.
#(for %%i in (*.txt) do #if /i not "%%~i" == "content.txt" #echo %%i;)>"content.txt"
if the plan is to iterate through subdirs as well, run for /R
I found a partial solution to list subfolders, excluding parent path. It is not perfect (for loop is not accurate as mentioned in above comments, neither I am happy with pushd command), but works. I left "space" in place of \ because thats how I need it.
#echo off
setlocal enabledelayedexpansion
pushd "c:\users\documents\"
(
for /r %%a in (*.txt) do (
set x=%%a
set x=!x:%cd%=!;
echo !x:\= !
)
)>filelist.log
popd
Code explanation:
First I remove the parent path with set x=!x:%cd%=!; (exclamation marks replace the %, see enabledelayedexpansion help) and then remove the slashes when echoing.

Adding a header into multiple .txt files

I need a batch file that will add the text "Write In" in a new line at the beginning of the content of hundreds of .txt files without removing any existing text. I found something on here that did not work for me. Anyone have suggestions?
This is the code I was working with:
for /r %%a in (*.txt) do (
echo ---- %%a before ----
type "%%a"
echo --------------------
echo Write In > "%%a.tmp"
type "%%a" >> "%%a.tmp"
del "%%a"
move "%%a.tmp" "%%a"
echo ---- %%a after ----
type "%%a"
echo --------------------
)
pause
It did nothing
I would most probably do it like this:
rem // Create temporary header file:
> "head.txt" echo Write In
rem // Iterate all text files in current directory:
for %%F in ("*.txt") do (
rem /* Combine header and currently iterated text file into a temporary file;
rem there cannot arise any file name conflicts (like temporary files becoming
rem iterated also unintendedly, or temporary files overwriting files to handle),
rem because the extension of the temporary files differ from the text files: */
copy /B "head.txt"+"%%~F" "%%~F.tmp"
rem // Overwrite original text file by temporary file, erase the latter:
move /Y "%%~F.tmp" "%%~F"
)
rem // Erase the temporary header file:
del "head.txt"
I found a way that is not very clean and will take a while for bigger files, but it works out for me:
#echo off
for /r "%~dp0" %%f in (*.txt) do (
echo Processing file %%f
>"%%~f.2" (
echo "Text to append here"
type "%%~f"
)
del "%%~f"
ren "%%~f.2" "%%~nxf"
)
pause
Loops recursively through all .txt -files in the batch-files directory.
Creates a new file with the name oldName.txt.2 and files it with the text to add and the rest of the oldfiles content.
Deletes the old file and renames the new file to the name of the old one.
The addition of .2 to the file ending is needed to make sure it does not get processed again by the loop.
Of course you can add multiple lines by multiplying the echo lines.
You can as well add text to the end of the file like this with adding the echo lines after the type line.
You can simply use the Linux Sed command to insert a header into a file.
sed -i '1s/^/This is my header\n/' filename
e.g. sed -i '1s/^/Write In\n/' myfile.txt
This can be applied to multiple files under a directory:
For .txt files
for file in *.txt
do
sed -i '1s/^/This is my header\n/' $file
done
For CSV files
for file in *.csv
do
sed -i '1s/^/This is my header\n/' $file
done

I need to delete a string in multiple lines from a file using batch script

Working file: Projects/fentbase/common/javasrc/validators/PasswordSetupVal.java
Working file: Projects/fentbase/channeladministration/spec/ui/dev/ppdl/AccessSchemeMaintenancePreview.dppdl
Working file: Projects/fentbase/common/javasrc/validators/EmailValidator.java
Working file: Projects/fentbase/common/javasrc/validators/MailIdVal.java
I have this in a file. I need to take out "Working file: " from every line. Please let me know how do I do that.
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%1) do (
set line=%%a
echo !line:Working file=!
)
Usage:
sciprt.cmd file.txt > new_file.txt
move /y new_file.txt file.txt
another way to skin the cat:
for /f "tokens=2,*" %%i in ('type "file.txt"') do #echo(%%j
Here is one way:
type "file.txt" | repl "Working file: " "" L >"newfile.txt"
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.

search multiple strings in multiple files via command-line

I have a txt file that contains about 500 values, one per line. I need to check to see of any of those 500 values appear in any of 6 csv files each containing 100k lines. I can search for one value in those 6 csv files using
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
but how do I do multiple searches automatically via command-line or batch file (CaSe SenSiTIve)?
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=C:\destdir"
for /f "delims=" %%a in ('dir /s /b "%sourcedir%\*.csv"') do (
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt"
GOTO :EOF
What you are asking is rather unclear. I used c:\sourcedir as the location of the .csv files and c:\destdir as the location for the reports. Replacing
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt with your original (with the double > would accumulate the lines into a single file - if that's what you want. As it stands, a new file will be created with name the same as your .csv+found.txt
An easy way is to use a batch script. You can loop through each of the files one by one. If you want to do them all at once you need to thread your program.
for /L %%A in (1,1,6) do (
Your code goes here
)
That batch script will loop six times. I am not really sure how you specify the file but if you loop through each file it will work.
So put your current batch script where I said "Your code goes here"
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
But you need to edit it to point to the file that you want to search. If your files are 1.txt, 2.txt 3.txt then all you need to do is set your file name to the current loop iteration number.
I've been using variants of this shell function for years:
ematch () {
for f in $(find . -type f | grep -v '~' | grep -v \.svn\/) ; do
egrep "$1" "$f" /dev/null 2> /dev/null
done
}
-> ematch "(string1|string2|string3)"
Feel free to adapt to your needs and post your mods here.

How to write a batch file to find a string in a text file and push it into a new text file

i want to write a batch file to find a particular string in a file. If the string is found then i want to redirect the whole line which contains the string to another file. for ex:
suppose a file myfile.txt contains the following text
abcwerthfdh
qwerewtretywr
weqreqwrtabcwerwe
wqerweqabcqwewq
when i launch the batch file giving myfile.txt and abc as command line arguments, then the output should be into a file called newfile.txt and it should contain only the lines which contain the text "abc". if i run this code again it should append to newfile.txt and not delete existing contents. in this case it should push the lines 1, 3 and 4 into newfile.txt
#echo off&setlocal
for /f "delims=" %%a in ('findstr "%~2" "%~1"') do (echo(%%a)>>newfile.txt
You could use FINDSTR:
FINDSTR abc myfile.txt >> newfile.txt
type myfile.txt | findstr /n "abc" >> newfile.txt
???

Resources