Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with renaming file!

Status
Not open for further replies.

katbear

Programmer
Mar 14, 2007
270
US
Hi,

This is not a sql server question, per se, but everyone here is so helpful I figured it would be a good place to ask!

I have an extract that is named like:

myFile.txt.pgp.20070820

It's stupid because the timestamp is AFTER the .pgp extension. However, that is how it is and I can't change it.

The problem is, when I attempt to decrypt the file using my custom batch script, it just plain IGNORES the file if it doesn't end in .pgp. That is, it doesn't work. If I remove the timestamp portion, it's fine.

I don't know how to make my batch file work around this problem. So I figure I must work around the file name by changing it first!

Is there a way to rename this file using batch commands so that it becomes:

myFile20070825.txt.pgp???

I'm desperate for a solution here, as it's holdings things up for me.

Thanks much
 
Yes using the FOR command. I can provide an example if you'd like.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Something along these lines/
Code:
cd \Folder\With\File
@FOR /F "delims=. tokens=a,b,c,d" %a IN (`dir /b *.pgp.*`) DO rename %a.%b.%c.%d %a%d.%c.%d

If I've written this correctly (not tested) it will rename all files which have *.pgp.* as a filename to the format you specified. IE "myfile.txt.pgp.20070815" will become "myfile20070815.txt.pgp".

Note the backward single quotes (on the ~ key) around the dir command. You may need to put two % signes everywhere that I have one % sign. Different OSs handle this differently, it's very annoying.

From a command line type "help for" to get the full syntax of the command.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
It's giving me an error:

a.c.ac.d was unexpected at this time.
 
I made a couple of changes. Looks like I was using the wrong single quotes and needed to put the %% in place of the %. Also I had the tokens wrong.
Code:
cd \Folder\With\File
@FOR /F "delims=. tokens=1,2,3,4" %%a IN ('dir /b *.pgp.*') DO rename %%a.%%b.%%c.%%d %%a%%d.%%b.%%c

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Hi,

I figured it out :)

for /f "tokens=1,2,3,4 delims=." %%a in ('dir *.pgp.* /b') do rename %%a.%%b.%%c.%%d %%d_%%a.%%b.%%c

I was also in the wrong directory.

Thanks for your help!!!
 
Cool.

The correct directory does help out.

No problem.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top