• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Truncate the Prefix of File Names

Hello,

I have scripted quite a numbert of SQL objects to a series of directories.  SQL prefaces all of the object files with "dbo.".  Does anyone know of a DOS command which will chop off this prefix?  I tried using RENAME but was unable to figure out how to indicate that I didn't want the first 4 characters.

This is a fairly common task and any way to do this programmatically would help immensly.  Thanks.

-Mari
0
Mariyam
Asked:
Mariyam
  • 5
  • 3
2 Solutions
 
bmedwardCommented:
What OS are you using?
0
 
bmedwardCommented:
If you are on XP (this should work back to 2k), put the following into a batch file and run it from the directory that has the files.
        @echo off
        dir /b dbo.* > tmp_dbo.dat
        REM for /f "tokens=1* delims=. " %%a in (tmp_dbo.dat) do echo  %%a.%%b %%b
        del tmp_dbo.dat

You will need to remove the REM for this to really work. This is just a safety thing.
0
 
bmedwardCommented:
Also note that this will clobber any existing files that you may have if they are named "tmp_dbo.dat"
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MariyamAuthor Commented:
I tried the batch but it didn't work.  I also tried working on just one file and executing line by line.  When I attempted to execute the line beginning 'for /f  "tokens=1* delims=. " %%a in...'  I received this error: "%%a was unexpected at this time."

It did delete the temp file though :-)

I can increase the points if you have time to research this for me please.  And Windows 2000 Server is running on the machine that I'm remoting into.

Thanks,
-Mari
0
 
GnarOlakCommented:
I changed the 'echo' to 'move' in the third line and it worked under WinXP.

To run the commands by hand change all occurances of '%%' to '%' in the third line and it should work.
0
 
bmedwardCommented:
sorry, not enough caffeine last night.
As noted above ... Batch files require the double percent sign (%%a), from the command line, a single one (%a) is needed.

I accidentally left it echoing the filenames instead of moving them. This was for my own debugging, as I do not have a folder full of dbo prefixed files and I wanted to be sure that it would not match any other occurrences of files.

GnarOlak's recommendation of changing the 'do echo blaa' to 'do move blaa' will make the line move the files instead of just echoing the filenames back to you.

Thanks to GnarOlak for catching that!
0
 
MariyamAuthor Commented:
Thank you both for you assistance :-)
0
 
MariyamAuthor Commented:
I forgot to ask, what exactly does this segment of the code do and is /f a switch of the FOR command?

' /f "tokens=1* delims=. " %%a in (tmp_dbo.dat) '

Thanks again.

-Mari
0
 
bmedwardCommented:
From a cmd.exe shell, type "for /?" for some help on the command's syntax. This one is kind of cryptic, the MS page has a little more info on it here. http://www.microsoft.com/windowsxp/home/using/productdoc/en/default.asp?url=/windowsxp/home/using/productdoc/en/for.asp 

The " /f " parameter tells " for " to parse the contents of the file.
"tokens=1*" and "delims=." describe how it will do the parsing.
Delims sets the parsing to split sections of text on every occurrence of the delimiter character " . ".
Tokens lets you define which occurrences how you want to assign to variables, and how you want to do so. "tokens=1*" sets the first match to the first variable, and then sets everything else on the line to a second variable. These variables are sequentially assigned based on the letter that you use in the “ for “ statement.

In this case, it lets you take the contents of the temp file, line by line. Then it breaks each line at the first period and assigns the beginning to %a and the rest to %b.

This is a nice little touch that MS added to batch scripting. And some still say that MS is only now starting to recognize Linux…
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now