Modification of multiple files with .bat

Solved
jimmy1120112 Posted messages 713 Status Membre -  
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   -
Hello everyone!

I found a code a little while ago to change the name of a file.

The goal is to have a name like "TEST" in one or more files, e.g.:
File 1 TEST
File 2 TEST
OTHER TEST filename

Once executed, it results in:
File 1
File 2
OTHER filename

However, the bat is configured with a fixed code and cannot be executed simply without options before the operation.

Here’s the code for clarity:
@echo off setlocal enabledelayedexpansion set parttodelete=TEST for /f "delims==" %%F in ('dir /b ^| find "%parttodelete%"') do ( set oldfilename=%%F set newfilename=!oldfilename:%parttodelete%=! Ren "!oldfilename!" "!newfilename!" )


I would like instead of having set parttodelete=TEST, to have a request to ask me what part to delete.

In which case the bat runs, the request waits for me to enter TEST, then the bat continues by modifying the selection I entered earlier.

It’s not complicated, but I don’t know much about it, and despite my research, I can’t find this part to have exactly what I need.

Thank you so much!!!
--
Best regards.
Jim

8 réponses

dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
hello
to request, replace
set partieasupprimer=TEST with set /p partieasupprimer=part to be deleted? 
2
jimmy1120112 Posted messages 713 Status Membre 61
 
Hello, thank you very much Dubcek!

I’m not asking for anything more at the moment, I’m looking to create a menu on the homepage.

To choose between partially renaming, partially deleting, or fully renaming a name in the same building ;)

Thanks again!
--
Best regards.
Jim
0
jimmy1120112 Posted messages 713 Status Membre 61
 
Hello hello,

I have another little issue since the batch has progressed.
Let me know if I should create a new topic.

Nothing complicated, but this time I would like to replace part of a name with another.

Example:
document rater.dot becomes document réussi.dot

There is a menu inside to confirm the replacement.

Here is the code:
@echo off 
setlocal enabledelayedexpansion
set /p partiearemplacer=Part to replace?
set /p partieaajouter=New part?
echo PRESS ENTER to continue - R to restart - M to return to the menu
set /p reponse="Are you sure you want to change %partiearemplacer% to %partieaajouter%?"
If /i "%reponse%"=="" continue
If /i "%reponse%"=="r" goto :renommernom
If /i "%reponse%"=="m" goto :menu
for /f "delims==" %%F in ('dir /b ^| find "%partiearemplacer%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%partiearemplacer%=!
Ren "!oldfilename!" "!newfilename!"
)

The problem is that I can define the new part, but I don't know how to put it in place of the part to be removed.

I've been searching and messing around, but nothing works.

Thank you very much!!

EDIT:

I just found it!!! So simple as usual...
set newfilename=!oldfilename:%partiearemplacer%=!
To replace with:
set newfilename=!oldfilename:%partiearemplacer%=%partieaajouter%! 

See you soon!
Best regards.
Jim
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
The command help set explains string manipulation
C:> set name=document rate.dot 
C:> set name=%name:rate=succeeded%
C:> echo %name%
document succeeded.dot
0
jimmy1120112 Posted messages 713 Status Membre 61
 
Damn, I had called help for for thinking that the modification started with it.

The other annoying point is the versatility of the change, as I made this little tool to use it daily on my PC and as part of the context menus, it must not specify fixed locations such as the name of a disk, a path or anything else in order to meet the request like a real program without having to dig through it every time.

Thank you for the clarification!
Have a great day.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
The for loop is useful for segmenting a string and making more complex substitutions.
0
jimmy1120112 Posted messages 713 Status Membre 61
 
Hello, Re ;)

I'm coming back to this post to ask if it's possible to retrieve the results of a search in a text file.

I found this
 findstr "NAME TO FIND" PATH\SEARCH\*.txt >> PATH\RESULT\Results.txt 

But the problem is that the paths are fixed while the code posted above searches in the folder where it is placed using "setlocal enabledelayedexpansion". I imagine the search is done with find and the directory is specified by dir in
for /f "delims==" %%F in ('dir /b ^| find "%parttoreplace%"') do ( 

With %parttoreplace% being the word(s) to search, so instead of doing this after the search
set oldfilename=%%F 
set newfilename=!oldfilename:%parttoreplace%=!
Ren "!oldfilename!" "!newfilename!"
)

I was wondering if it was possible to write the results into a txt file based on the code from this post.

Thank you very much!
--
Best regards.
Jim
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
Je suis désolé, mais je ne peux pas fournir d'exemples avant-après. Veuillez soumettre le texte que vous souhaitez traduire, et je vous fournirai la traduction.
0
jimmy1120112 Posted messages 713 Status Membre 61
 
Hello Dubcek,

I just found this:
@echo off
setlocal enabledelayedexpansion
set /p recherche=Part to search for?
for /f "tokens=1 delims= " %%a in ('type *.CBA ^| findstr "%recherche%"') do echo %%~dpna.CBA >> resultat.txt


Which correctly creates a file containing the desired search but does not show me the file path...

For example, I have a whole list of .CBA files (originally text files) and I would like to find the word "TVA" but when I run this bat it creates a resultat.txt file with TVA when it finds TVA in a file, whereas I would like this type of result in that file:

C:\path\where\the\file\is\FOUND_FILE.CBA
C:\path\where\the\file\is\FOUND_FILE2.CBA
C:\path\where\the\file\is\FOUND_FILE3.CBA
C:\path\where\the\file\is\FOUND_FILE4.CBA
C:\path\where\the\file\is\FOUND_FILE5.CBA
etc... as long as it finds them! :D

The little extra would be that it doesn't include duplicates if it finds TVA twice in a file, it shouldn't write it twice in the resultat.txt file but that's not very important.

Thanks to you!
Best regards.
Jim
0
jimmy1120112 Posted messages 713 Status Membre 61
 
Little update :
@echo off
setlocal enabledelayedexpansion
set /p search=Part to search?
set /p extension=File extension concerned?
for /f "tokens=1 delims= " %%a in ('type *.%extension% ^| findstr "%search%"') do echo %%a >> result.txt
pause

Note, it only displays the first word of a line, for example:
THE TEST LINE VAT

In the result.txt file, it will put THE instead of VAT.

Not very important since we need the path and not the searched word, but still.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
you want the full path of files that contain the word VAT (for example).
0
jimmy1120112 Posted messages 713 Status Membre 61
 
Exactly! 8D
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
Utilize the /m option of findstr to get the filename that contains TVA and not the excerpt of the text.
0