Remove space ad add prefix to all files in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Remove space ad add prefix to all files in a folder

#1 Post by ceccozambu » 20 Dec 2017 10:58

Hi,
I’ve an issue with a batch file.

I would create a batch for:

In a folder es C:\origin I have a lot of file like:
Aa 1234.txt
bb 3456.jpg
cc122 443.doc
Ddeeff gg.xls

the program should move the file to C:\origin\temp and remove the space from the name of file and add a prefix “Pre_” before name. So the result should be:
C:\origin\temp\Pre_Aa1234.txt
C:\origin\temp\Pre_bb3456.jpg
C:\origin\temp\Pre_cc122443.doc
C:\origin\temp\Pre_Ddeeffgg.xls

I did the first part of program but i don't know how to do the rest.

Code: Select all


@echo off
echo if "%%3"=="" if not "%%2"=="" ren "%%1 %%2" %%1%%2
lfnfor on
set "=
for %%f in (*.*) do call ~nospace.bat %"%%%f%"%


Someone can help me?

Thanks in advance

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Remove space ad add prefix to all files in a folder

#2 Post by Squashman » 20 Dec 2017 11:13

Code: Select all

@ECHO OFF
setlocal enabledelayedexpansion
REM Change the working directory
PUSHD "C:\Origin"
REM Get all files with an extension
for %%G in (*.*) do (
	set "name=%%~G"
	REM remove spaces from name
	set "name=!name: =!"
	REM move file to temp with new name
	move "%%~G" "temp\Pre_!name!"
)
Rem change back to the previous directory.
POPD
Last edited by Squashman on 20 Dec 2017 11:19, edited 1 time in total.
Reason: Forgot to enable delayed expansion.

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#3 Post by ceccozambu » 21 Dec 2017 05:03

You are super! Thank you very much!! :)

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#4 Post by ceccozambu » 29 Dec 2017 02:55

Hi,
can you please help me again? :-)

I’ve to create another procedure to remove the prefix “Pre_” to all file in folder “C:\Project\Temp\” and move to folder “C:\Project\Save\”.
Example:
C:\Project\Temp\Pre_File1.jpg
C:\Project\Temp\Pre_File2.doc
C:\Project\Temp\Pre_File3.txt

Result:
C:\Project\Save\File1.jpg
C:\Project\Save\File2.doc
C:\Project\Save\File3.txt

Thanks,
Francesco

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#5 Post by ceccozambu » 29 Dec 2017 05:42

I tried with this but it doesn't work

Code: Select all

for %%i in ("*.*") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off 1st four chars
ren "%fname%" "%fname:~4%"
goto :eof

Can you help me?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Remove space ad add prefix to all files in a folder

#6 Post by aGerman » 29 Dec 2017 06:21

If you want to move files you have to use the MOVE command.

Code: Select all

for %%i in ("*.*") do if /i "%%~xi" neq ".bat" (set "fpath=%%~fi" &set "fname=%%~nxi" &call :rename)
goto :eof
:rename
move "%fpath%" "C:\Project\Save\%fname:~4%"
goto :eof
Steffen

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#7 Post by ceccozambu » 29 Dec 2017 07:32

Hi, thank you very much.
Only a little problem, the circle don't work.
The program rename and move just one file.
Can you help me again?
Thanks in advance,
Francesco

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Remove space ad add prefix to all files in a folder

#8 Post by aGerman » 29 Dec 2017 07:44

I'm afraid the code moves the batch file, too :lol: Maybe you should just exclude *.bat files. I updated the code above.

Steffen

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#9 Post by ceccozambu » 29 Dec 2017 08:02

Thanks for help.
the renaming works well but not the moving of the file.
After process no files has been moved.
Can you help me?

Thanks,
Francesco

SIMMS7400
Posts: 544
Joined: 07 Jan 2016 07:47

Re: Remove space ad add prefix to all files in a folder

#10 Post by SIMMS7400 » 29 Dec 2017 08:08

Place an echo before the move and then a pause after to see what is being moved, or trying to be moved:

Code: Select all

for %%i in ("*.*") do if /i "%%~xi" neq ".bat" (set "fpath=%%~fi" &set "fname=%%~nxi" &call :rename)
goto :eof
:rename
echo move "%fpath%" "C:\Project\Save\%fname:~4%"
pause
goto :eof
This should help you debug.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Remove space ad add prefix to all files in a folder

#11 Post by aGerman » 29 Dec 2017 08:12

Can you help me?
No I can't help because I don't know where you saved the batch file.
You wrote that you want to move the files from C:\Project\Temp to C:\Project\Save
1) Both folders have to exist.
2) The batch file should be saved in C:\Project\Temp.

Steffen

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#12 Post by ceccozambu » 29 Dec 2017 08:49

Hi,
the folder and the file are correct and into correct place.
Into folder c:\project\temp there are: test.bat (with the code), Pre_aaaa.jpg and Pre_qqqq.jpg
When the batch run i get this, and after no file are moved and renamed.

___

C:\project\temp>for %i in ("*.*") do if /I "%~xi" NEQ ".bat" (set "fpath=%~fi" & set "fname=%~nxi" & call :rename )

C:\project\temp>if /I ".jpg" NEQ ".bat" (set "fpath=C:\project\temp\pre_aaaa.jpg" & set "fname=pre_aaaa.jpg" & call :rename )

C:\project\temp>echo move "C:\project\temp\pre_aaaa.jpg" "C:\Project\save\aaaa.jpg"
move "C:\project\temp\pre_aaaa.jpg" "C:\Project\save\aaaa.jpg"

C:\project\temp>pause
Press any key to continue . . .

C:\project\temp>goto :eof

C:\project\temp>if /I ".jpg" NEQ ".bat" (set "fpath=C:\project\temp\pre_qqqq.jpg" & set "fname=pre_qqqq.jpg" & call :rename )

C:\project\temp>echo move "C:\project\temp\pre_qqqq.jpg" "C:\Project\save\qqqq.jpg"
move "C:\project\temp\pre_qqqq.jpg" "C:\Project\save\qqqq.jpg"

C:\project\temp>pause
Press any key to continue . . .

___

Thanks in advance,
Francesco

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Remove space ad add prefix to all files in a folder

#13 Post by aGerman » 29 Dec 2017 09:19

The ECHO makes that the commands are only displayed instead of being executed. They look okay to me. So remove ECHO and PAUSE and it should work.

Steffen

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Remove space ad add prefix to all files in a folder

#14 Post by ceccozambu » 29 Dec 2017 09:27

It works! :D
Thank you very much!

Have a nice day and happy new year!

Francesco

Post Reply