Searching file and renaming folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Searching file and renaming folder

#1 Post by Shawn26 » 20 Dec 2013 07:43

Hello guys,

i need some help with my batch file.
I need a batch file that is searching recursiv for a special file.
The file which im am searching for looks like "*.txt".

If one text file was found in the folder the foldername should be renamed to "foldername [filename]".

for example:

folder 1\folder 2\file1.txt
the batch file finds the "file1.txt" in folder 1\folder 2 and renams the folder "folder 2" into "folder 2 [file1]".

This is my script so far.

Code: Select all

@echo off
   title Videosammlung Audiobearbeitung
   Color 2F

REM Eingabe der Dateiendung die gesucht wird.
   SET /P Dateiendung=Zu suchende Dateiendung eingeben:
   SET DATEI=*.%Dateiendung%

REM Rekursive Suche nach der Datei mit der eingegebenen Dateiendung und schreiben in ein Log-File.
   DIR /b /s %datei% > log.txt

PAUSE


I thought i could create a log-File and search in this log-File to get my result. But i am stuck.

Can anybody help.

Many thanks!

Shawn26

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching file and renaming folder

#2 Post by foxidrive » 20 Dec 2013 07:59

Code: Select all

@echo off
set "folder="
set "file="
for /r %%a in (file1.txt) do set "folder=%%~dpa"&set "file=%%~na"
if defined folder move "%folder%" "%folder [%file%]"

Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#3 Post by Shawn26 » 20 Dec 2013 08:57

Thanks for the answer.
But i get only error messages.

Der Befehl ""set folder=C:\temp\Privat\"" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Der Befehl ""set folder=C:\temp\Privat\Bilder\"" ist entweder falsch geschrieben
oder
konnte nicht gefunden werden.
Der Befehl ""set folder=C:\temp\Privat\Bilder\test\"" ist entweder falsch geschr
ieben oder
konnte nicht gefunden werden.
Der Befehl ""set folder=C:\temp\Privat\Tools\"" ist entweder falsch geschrieben
oder
konnte nicht gefunden werden.
Drücken Sie eine beliebige Taste . . .


Can you explain your code. That would be nice! Not everytime there is a file called "file1.txt". I will search for every "*.txt" File and if there is a "*.txt" File i will rename the folder into "folder [*]".

Many Thanks

Shawn26

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Searching file and renaming folder

#4 Post by AiroNG » 20 Dec 2013 09:18

wrong:
"set variabel="

correct:
set "variabel="

wrong:
for /r %%a in (file1.txt) do "set folder=%%~dpa"&set "file=%%~na"

correct:
for /r %%a in (file1.txt) do set "folder=%%~dpa"&set "file=%%~na"

foxidrive just misplaced one of the quotation marks (Anführungszeichen).

Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#5 Post by Shawn26 » 20 Dec 2013 09:33

Thanks again for an fast answer.
But i have still problems with the script.

Now i changed the misplaced quotation marks.
The new script looks like.

Code: Select all

@echo off
set "folder="
set "file="

for /r %%a in (hinweis.txt) do set "folder=%%~dpa"&set "file=%%~na"
if defined folder move "%folder%" "%folder [%file%]"

PAUSE


This is the result.

Das System kann die angegebene Datei nicht finden.
Drücken Sie eine beliebige Taste . . .


Any ideas?

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Searching file and renaming folder

#6 Post by AiroNG » 20 Dec 2013 09:41

where is your "hinweis.txt" located?
if it's not in the same folder you need to declare the path where it is.

example:

Code: Select all

for /r %%a in (C:\Pfad\zur\Datei\hinweis.txt) do set "folder=%%~dpa"&set "file=%%~na"
if defined folder move "%folder%" "%folder [%file%]"


edit:
If there's a space in a foldername you have to use quotation marks.
ie.: for /r %%a in ("C:\Pfad\zur\Datei\hinweis.txt") [...]

Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#7 Post by Shawn26 » 20 Dec 2013 09:53

But thats the problem.

1) I will search for many ".txt" Files not only for one.
It can be "hinweis.txt" , "test.txt", and so on.

2) I will search in many subfolders. not only one folder.

Thats the changed code....

Code: Select all

@echo off
set "folder="
set "file="

for /r %%a in (C:\temp\Privat\Tools\hinweis.txt) do set "folder=%%~dpa"&set "file=%%~na"
if defined folder move "%folder%" "%folder [%file%]"

PAUSE


and thats the result. There a still errors.

Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung
ist falsch.
Drücken Sie eine beliebige Taste . . .


many thanks for helping me.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching file and renaming folder

#8 Post by foxidrive » 20 Dec 2013 23:20

@AiroNG thanks for spotting my error.

@Shawn26 Your task is to rename every folder with a .txt file inside it, over a whole directory tree, right?

Run this and examine the tempren.bat.txt file that is created. Check to see that it is doing what you need.
if there are multiple *.txt files in a folder then it will move the folder to the first name, and ignore the rest.

If you are happy with it then rename tempren.bat.txt to .bat and run it.

test it on some sample folders first

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /b /s /ad ^|sort /r') do (
    for %%b in ("%%a\*.txt") do >>tempren.bat.txt echo if exist "%%a\" move "%%a" "%%a [%%~nb]"
)
pause

Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#9 Post by Shawn26 » 23 Dec 2013 04:28

PERFECT!!!

That works totally fine. That is what i want and need.

I need two things more.

1.) I want to define the file that the batch has to search for at the beginning.
Maybe keyboard entries. (a.e. The batch asks "What file you are looking for?" and i type in ("txt").

What do i have to do to write an log File what was done in the background and save this .log File in the same directory like the .bat File.

Many Thanks!

Shawn26

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching file and renaming folder

#10 Post by foxidrive » 23 Dec 2013 04:50

Shawn26 wrote:1.) I want to define the file that the batch has to search for at the beginning.
Maybe keyboard entries. (a.e. The batch asks "What file you are looking for?" and i type in ("txt").

What do i have to do to write an log File what was done in the background and save this .log File in the same directory like the .bat File.


It's not difficult to do the first thing - give it a shot.

As for the second thing, the file that is being created is a log file itself. It shows what is being done.

Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#11 Post by Shawn26 » 23 Dec 2013 05:10

I changed the code into this. Cause i dont want a second .bat to take my result.
What i have to do to get a .log file with this bat?

The rest of my question is solved.

Code: Select all

@echo off
title Videosammlung Audiobearbeitung
   Color 2F

REM Eingabe der Dateiendung die gesucht wird.
   SET /P ENDUNG=Zu suchende Dateiendung eingeben:
   

for /f "delims=" %%a in ('dir /b /s /ad ^|sort /r') do (
    for %%b in ("%%a\*.%ENDUNG%") do if exist "%%a\" move "%%a" "%%a [%%~nb]"
)


Many Thanks!

Shawn26

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching file and renaming folder

#12 Post by foxidrive » 23 Dec 2013 05:16

Code: Select all

@echo off
title Videosammlung Audiobearbeitung
   Color 2F

REM Eingabe der Dateiendung die gesucht wird.
   SET /P ENDUNG=Zu suchende Dateiendung eingeben:
   

for /f "delims=" %%a in ('dir /b /s /ad ^|sort /r') do (
    if not exist "%%a\*.%ENDUNG%" >>"file.log" echo No Action: "%%a"
    for %%b in ("%%a\*.%ENDUNG%") do if exist "%%a\" (
       move "%%a" "%%a [%%~nb]"
       >>"file.log" echo Action: "%%a" moved to "%%a [%%~nb]"
       )
)


Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#13 Post by Shawn26 » 23 Dec 2013 05:30

Many thanks!

It works fine. Is it a big problem to write also all folder without any of the defined files.

May i have three folder.
1) folder1 (withouth any .txt file in it)
2) folder2 (with a .txt file in it)
3) folder3 (with a .txt file in it)

The batch has to search for ".txt" files and in the log file i can find something like that.

Actions:
"C:\temp\folder2" moved to "C:\temp\folder2 [textfile]"
"C:\temp\folder3" moved to "C:\temp\folder3 [textfile]"

No Actions:
"C:\temp\folder1"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching file and renaming folder

#14 Post by foxidrive » 23 Dec 2013 10:03

Shawn26 wrote:@foxidrive:

many thanks for helping. but i think there is a mistake.
The Batch creates a log File but in the Log-File there is only one entrie.


Yes, you are right, my logic was flawed. Try the code above now as I edited it again.

Shawn26
Posts: 9
Joined: 20 Dec 2013 07:27

Re: Searching file and renaming folder

#15 Post by Shawn26 » 24 Dec 2013 03:12

Now it will be a little bit difficult.
I have many problems to do the following things and hope that anybody (Maybe foxidrive :) ) can help me.

The script works perfect. But i need a little bit more.
This is my structure.

Folder - Subfolder 1 - Videofile
Folder - Subfolder 2 - Videofile
Folder - Subfolder 3 - Videofile


1) The script should analyze with mediainfo_cli every videofile in every subfolder and create a .txt file with the Audio Informations (a.e. there is a videofile with DTS sound the script has to create a DTS.txt file, if the videofile has a AC3 6 Channel sound there has to be a AC3-6.txt).
After that Analyzing the script should work as it do right now.


Batch Processing Example
If a file C:\video\USA Holiday.mpg contains AC-3 audio, then this may be determined and used in the following C:\conversion.bat called using:

C:\conversion.bat C:\video\USA Holiday.mpg

---conversion.bat--------------------------------------------------
@echo off
: Get the codec
C:\Programs\mediainfo_cli\mediainfo.exe --Inform=Audio;%%Codec%% %1 > C:\value.txt
: Read it
for /f "tokens=*" %%A in ('type C:\miresult.txt') do set myVar=%%A
: Test for AC-3
if ".%myvar%" == ".AC-3" goto :AC3
: non-AC-3 processing follows
ffmpeg.....
goto :exit
:AC3
: AC-3 processing follows
ffmpeg....
:exit

Post Reply