Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

batch script to replace filenames h

Original Message
Name: jan (by janita)
Date: March 13, 2008 at 09:50:29 Pacific
Subject: batch script to replace filenames h
OS: windows 2000
CPU/Ram: 512
Model/Manufacturer: vectra 400
Comment:
Hi,

I need to replace filenames having spaces and dots with underscre using a .bat script

example:

need to convert filename like RBS Diff data we 06.07.07.xls to RBS_Diff_data_we_06_07_07.xls

Can someone kindly help?



Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: March 13, 2008 at 09:58:12 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Run the following batch in the folder holding the files to be renamed

@echo off
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename=.=_!
set filename=!filename= =_!
if not !filename!==%%~nj ren %%j !filename!%%~xj
)


Report Offensive Follow Up For Removal

Response Number 2
Name: jan (by janita)
Date: March 13, 2008 at 10:08:58 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
The filename has been completely blanked out
can u help plz?

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: March 13, 2008 at 10:18:05 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Sorry, I'm looking to fix the problem.
See you as soon as possible.

Report Offensive Follow Up For Removal

Response Number 4
Name: IVO
Date: March 13, 2008 at 10:21:45 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
This is right now, sorry again and I hope you had no harm

@echo off
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename:.=_!
set filename=!filename: =_!
if not !filename!==%%~nj ren %%j !filename!%%~xj
)



Report Offensive Follow Up For Removal

Response Number 5
Name: jan (by janita)
Date: March 14, 2008 at 02:53:25 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi,

Some files have worked and others have not

processedfile.231107 has been successfully transformed to processedfile_231107.

Thanks a lot

processedfile 11 Nov07 and FFL CTT Norm 07.11.07 has not been modified :(

Basically I require dots and spaces anywhere in the filename to be modified to _

Regards,
Jan


Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: March 14, 2008 at 04:19:55 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

Sorry again and again and...

Usually I don't release code without carefully testing it before, but this time I was very busy and too confident in myself.
So the first miscoded script and now another bug.

Replace

if not !filename!==%%~nj ren %%j !filename!%%~xj

with

if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"

Let me know if now we are saved.

Ciao from Milano - Italy (EU)


Report Offensive Follow Up For Removal

Response Number 7
Name: jan (by janita)
Date: March 14, 2008 at 04:42:32 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Thanks IVO. It has worked perfect :)

Actually I needed to change these file names bcoz I had to find if these files were in certain folder and if so either do some processing or delete them.

But when spaces and dots are present I cudnt use the wildcard * to find files starting with a particular name bcoz after the space or dot it does not recognise the filename and always says file not found. I tried enclosing in quotes as well

So thought of renaming and continue with my script.
Is this a fine idea? or do u have a simpler solution of finding filenames with spaces and dots.

Below is the deletion script i wrote for any filename starting as SSL. It works fine if there are no dots and spaces. Otherwise not!!

@ECHO OFF
SET PATH="C:\DEL\Program Files\Data\BCL\Processed"
SET FILENAME=SSL
SET FILELIMIT=20

if exist %PATH%\%FILENAME%*.txt (
For /F "TOKENS=1,2* DELIMS=[]" %%A IN ('DIR %PATH%\%FILENAME%*.txt /B /O:-D /T:C ^| find /N /I ".txt"') Do IF %%A GTR %FILELIMIT% DEL /F %PATH%\%%B

@ECHO "File Cleared on:" %Date% "at:" %Time% > %PATH%\BCL_Deletion.log

@ECHO "File Cleared on:" %Date% "at:" %Time%

exit 0

)

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 8
Name: IVO
Date: March 14, 2008 at 04:53:45 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

I'll take an accurate look at your script and task, but please be patient as now I have a hot call pending, then a meeting so you have to wait.

If I can, I'm glad to support you.

See you as soon.

Ivo Luigi Bertani


Report Offensive Follow Up For Removal

Response Number 9
Name: jan (by janita)
Date: March 14, 2008 at 04:56:54 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
No issues at all...
I will wait and thanks for all your help.
much appreciated.

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 10
Name: IVO
Date: March 14, 2008 at 07:36:27 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

here the working (I hope fine) version of the script you posted. You messed up with quotes and never use a reserved system word for your own variables (PATH).

@Echo Off

Set myPath=C:\DEL\Program Files\Data\BCL\Processed
Set FileName=SSL
Set FileLimit=20

If exist "%myPath%\%FileName%*.txt" (
Echo.
For /F "tokens=1,2 delims=[]" %%A in ('Dir "%myPath%\%FileName%*.txt" /B /O:-D /T:C ^| Find /N /I ".txt"') Do (
If %%A gtr %FileLimit% (
Del /F "%myPath%\%%B"
Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%myPath%\BCL_Deletion.log"
Echo. File "%%B" Cleared on: %Date% at: %Time%
)
)
)

Set myPath=
Set FileName=
Set FileLimit=
:: End_Of_Batch

If you need more support in developing your scripts, contact me freely using e-maail too.
Ever glad to help when time allows.

Ciao
Ivo


Report Offensive Follow Up For Removal

Response Number 11
Name: jan (by janita)
Date: March 14, 2008 at 08:25:55 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
HI IVO,

The spaces and dots still seem to be a problem with the above provided code :(

None of the files were deleted

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 12
Name: IVO
Date: March 14, 2008 at 08:49:17 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

too fast too many mistakes... again even if I partially tested the code this time.

I'll review the script and post the correct one as soon as possible.

To be continued...


Report Offensive Follow Up For Removal

Response Number 13
Name: jan (by janita)
Date: March 14, 2008 at 09:05:30 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
HI IVO,

The spaces and dots still seem to be a problem with the above provided code :(

None of the files were deleted

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 14
Name: ghostdog
Date: March 15, 2008 at 01:39:23 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
if you can use gawk , download from here: http://gnuwin32.sourceforge.net/pac...


{
file=FILENAME
gsub(/ +/,"_",file)
newfile=file
cmd="copy \042" FILENAME "\042 "newfile
system(cmd)
}

save the above as script.awk (including the braces ). from the command line

c:\test> gawk -f test1.awk "RBS Diff data we 06.07.07.xls"

change "copy" to "ren" if you want to rename the file instead.


Report Offensive Follow Up For Removal

Response Number 15
Name: IVO
Date: March 16, 2008 at 10:38:40 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

are you sure that more than 20 files with name starting SSL exist in your directory?

I carefully tested my script and it always worked fime either with names embedding dots or spaces and with a directory structure exactly duplicating what you posted. Just lowered the FileLimit to two for testing purposes.

So, please, verify what I said above to help me if bugs have to be fixed.


Report Offensive Follow Up For Removal

Response Number 16
Name: Mechanix2Go
Date: March 16, 2008 at 15:56:07 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
First if all, resetting a 'system' variable like PATH is a bad idea.

If the purpose here is to keep the 20 newest files, try this:

::==
@echo off
setLocal EnableDelayedExpansion

pushd c:\files

for /f "tokens=* skip=20 delims= " %%a in ('dir/b /o-d SSL*.XLS') do (
echo del "%%a"
)


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 17
Name: jan (by janita)
Date: March 17, 2008 at 07:16:34 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi IVO,

Your solution has worked perfect for me. Sorry the extension was .csv and i tried the same script you sent (.txt) and thought it dint work...

Thank you for the solution :)

Thanks to M2 and ghostdog for trying to help me as well

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 18
Name: jan (by janita)
Date: March 18, 2008 at 06:29:58 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi,

Reg the above solution provided am facing another problem

I am unable to delete files from folder named C:\DEL\Program Files\S&M_Data_Surplus

The folder name is not recognised after the & symbol

Error Message is "C:\DEL\Program Files\S
The system cannot find the path specified.

The code I use is as below

@ECHO OFF

Set IFACEPATH=C:\DEL\Program Files\Accurate NXG\S&M_Data_Surplus
Set FILENAME=SLM
Set FILELIMIT=2

echo "%IFACEPATH%\%FILENAME%*.csv"
If exist "%IFACEPATH%\%FILENAME%*.csv" (
For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\%FILENAME%*.csv" /B /O:-D /T:W ^| Find /N /I ".csv"') Do (
If %%A gtr %FILELIMIT% (
Del /F "%IFACEPATH%\%%B"
Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\S&M_Data_Surplus_Deletion.log"
Echo. File "%%B" Cleared on: %Date% at: %Time%
)
)
)
Echo. ********************************************************************************* >> "%IFACEPATH%\S&M_Data_Surplus_Deletion.log"


Set IFACEPATH=
Set FILENAME=
Set FILELIMIT=
:: End_Of_Batch

Kindly help

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 19
Name: IVO
Date: March 18, 2008 at 08:03:36 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
The problem you face is due to the weird char & that in NT batch has the special meaning of joining to commands on the same line.

In minutes I post the slightly modified code of the script to work around that. Be a bit patient because it is a chinese work to set up the changes required.


Report Offensive Follow Up For Removal

Response Number 20
Name: IVO
Date: March 18, 2008 at 08:36:28 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Here the modified code that enables dynamic variables marked with ! instead of the conventional % and uses the caret (^) to deactivate special & symbol inside variables.
Beware I have not tested it.

@Echo Off
SetLocal EnableDelayedExpansion

Set IFACEPATH=C:\DEL\Program Files\Accurate NXG\S^&M_Data_Surplus
Set FILENAME=SLM
Set FILELIMIT=2

Echo "!IFACEPATH!\!FILENAME!*.csv"
If exist "!IFACEPATH!\!FILENAME!*.csv" (
For /F "tokens=1,2 delims=[]" %%A in ('Dir "!IFACEPATH!\!FILENAME!*.csv" /B /O:-D /T:W ^| Find /N /I ".csv"') Do (
If %%A gtr !FILELIMIT! (
Del /F "!IFACEPATH!\%%B"
Echo. File "%%B" Cleared on: %Date% at: %Time% >> "!IFACEPATH!\S&M_Data_Surplus_Deletion.log"
Echo. File "%%B" Cleared on: %Date% at: %Time%
)
)
)
Echo. ********************************************************************************* >> "!IFACEPATH!\S&M_Data_Surplus_Deletion.log"
:: End_Of_Batch


Report Offensive Follow Up For Removal

Response Number 21
Name: jan (by janita)
Date: March 19, 2008 at 04:46:20 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi IVO,

The above solution has worked except that I replaced the ! with the % bcoz with ! it din work.

Thank you very much for the solution.

I have a final trouble with my code.
With the working logic you provided I tried to write scripts to delete all files (any file name and any extension) from the errors folder I have.

This doesnt seem to work.

Plz find below the code I use.

@ECHO OFF

Set IFACEPATH=C:\DEL\Program Files\Errors
Set FILELIMIT=2

echo "%IFACEPATH%\*.*"
If exist "%IFACEPATH%\*.*" (
For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\*.*" /B /O:-D /T:W ^| Find /N /I ".*"') Do (
If %%A gtr %FILELIMIT% (
Del /F "%IFACEPATH%\%%B"
Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\Errors_Deletion.log"
Echo. File "%%B" Cleared on: %Date% at: %Time%
)
)
)
Echo. ********************************************************************************* >> "%IFACEPATH%\Errors_Deletion.log"


Set IFACEPATH=
Set FILENAME=
Set FILELIMIT=
:: End_Of_Batch

Can you help fix the problem?

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 22
Name: jan (by janita)
Date: March 19, 2008 at 05:44:18 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi IVO,

The above solution has worked except that I replaced the ! with the % bcoz with ! it din work.

Thank you very much for the solution.

I have a final trouble with my code.
With the working logic you provided I tried to write scripts to delete all files (any file name and any extension) from the errors folder I have.

This doesnt seem to work.

Plz find below the code I use.

@ECHO OFF

Set IFACEPATH=C:\DEL\Program Files\Errors
Set FILELIMIT=2

echo "%IFACEPATH%\*.*"
If exist "%IFACEPATH%\*.*" (
For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\*.*" /B /O:-D /T:W ^| Find /N /I ".*"') Do (
If %%A gtr %FILELIMIT% (
Del /F "%IFACEPATH%\%%B"
Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\Errors_Deletion.log"
Echo. File "%%B" Cleared on: %Date% at: %Time%
)
)
)
Echo. ********************************************************************************* >> "%IFACEPATH%\Errors_Deletion.log"


Set IFACEPATH=
Set FILENAME=
Set FILELIMIT=
:: End_Of_Batch

Can you help fix the problem?

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 23
Name: IVO
Date: March 19, 2008 at 07:22:32 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Replace

Find /N /I ".*"

with

Find /N /V ""


Report Offensive Follow Up For Removal

Response Number 24
Name: jan (by janita)
Date: March 19, 2008 at 08:09:01 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi IVO,,

There is a folder within the errors folder. will this be taken into account with the filelimit
while deletion?

There always seems to be one file short while running this script.

I mean if I need only the last 6 files to remain only the last 5 files plus the folder remains.

Is there a work around or can I simply increase the filelimit by one?


Also I need to display the current date and time in my log files in this format

2008-03-19 12:07:07 6 files deleted

Is this possible?

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 25
Name: IVO
Date: March 19, 2008 at 08:17:46 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

I'll answer to all your questions, but later as now I have to leave. Tomorrow you'll find the solutions you need. Be patiet again.

Ciao for now
Ivo


Report Offensive Follow Up For Removal

Response Number 26
Name: IVO
Date: March 19, 2008 at 15:31:14 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

the switch /A:-D-H in the DIR command should solve the extra folder issue. The following code takes into account the number of files deleted too, but for the date and time I need you give me your local format as date and time are country dependent (in Italy we use DD/MM/YYYY HH.MM.SS in 24h format not at all the US format). So please post how the script displays and I will give you the layout you need.

Sorry today I'm at customer's company so I can't replay for the whole working day. If you post what I need, Friday you'll find date and time formatted.

Have a nice day

@ECHO OFF
Set IFACEPATH=C:\DEL\Program Files\Errors
Set FILELIMIT=2
Set COUNT=0

Echo "%IFACEPATH%\*.*"

If exist "%IFACEPATH%\*.*" (
For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\*.*" /B /A:-D-H /O:-D /T:W ^| Find /N /V ""') Do (
If %%A gtr %FILELIMIT% (
Del /F "%IFACEPATH%\%%B"
Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\Errors_Deletion.log"
Echo. File "%%B" Cleared on: %Date% at: %Time%
Set /A COUNT+=1
)
)
)
Echo.*** %date% - %time% : %COUNT% file(s) deleted *** >> "%IFACEPATH%\Errors_Deletion.log"
Echo.*** %date% - %time% : %COUNT% file(s) deleted ***

Set IFACEPATH=
Set FILENAME=
Set FILELIMIT=
Set COUNT=
:: End_Of_Batch


Report Offensive Follow Up For Removal

Response Number 27
Name: jan (by janita)
Date: March 20, 2008 at 05:24:08 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi IVO,

The script displays in this format

Thu 20/03/2008 - 12:22:51.50

and I would like it to display in the format as below

2008-03-19 12:07:07

Thanks for your help

Regards,
Jan


Report Offensive Follow Up For Removal

Response Number 28
Name: IVO
Date: March 21, 2008 at 04:01:36 Pacific
Subject: batch script to replace filenames h
Reply: (edit)
Hi janita,

here I'm back! I assume the count of files is done, so below the code to format your date and time. Replace the bottom part of the script with

Set LOGDATE=%date:~-04,4%
Set LOGDATE=%LOGDATE%-%date:~-07,2%
Set LOGDATE=%LOGDATE%-%date:~-10,2%
Set LOGTIME=%time:~0,-3%

Echo.*** %LOGDATE% - %LOGTIME% - %COUNT% file(s) deleted *** >> "%IFACEPATH%\Errors_Deletion.log"
Echo.*** %LOGDATE% - %LOGTIME% - %COUNT% file(s) deleted ***

Set IFACEPATH=
Set FILENAME=
Set FILELIMIT=
Set COUNT=
Set LOGDATE=
Set LOGTIME=
:: End_Of_Batch

Please, report if anything goes wrong as I had to simulate your formats to test the added statements.

Just curious, where do you live and work?

Ciao
Ivo


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: batch script to replace filenames h

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC