#1 21 Jan 2007 10:26

omnikron
Member
Registered: 21 Jan 2007
Posts: 4

rename a file with the string given by a volume's label

Hi.
First post here & allow me state with no conceit that I wish I had found your site earlier!
Attempting to master working with the built in XP commands is no fun when you only have microsofts pathetic A-Z pages.

Even with your great explanations I am in a jumble when attempting to do the following:

I want to insert a CD or DVD into one of the two optical drives on the PC on which the script (or 'batch file') will be run.

Then I want to create a file with alphabetically sorted list of all the files directories & subdirectories.

dir R: /s /o:gn>N:contentlisting00.txt

So far so good.


Then, at this point, my lack of grasping how parameters, variables & strings are handled by the commands, shows.
I cannot rename the file, 'contentlisting00.txt', using the disc's Volume Label.

I have managed to write the strings output by the command 'Vol' to a file.

I can then select the line I want from the two line output using 'find'.

I don't understand how to pipe/filter the output to 'Set' command so I can use the string handling outlined in this page:
http://ss64.com/nt/syntax-replace.html
to extract solely the text for the label.

Because of this there is no way I can pass this to the rename or an env variable such as '_renlabel' or something with a more transparent name.


You will inevitably suggest a more direct route using some facet of a command which may be unknown to me.

I would still like to know, (if at all possible):
o How can you assign strings & numerical values to an env variable of your choosing?

o How you are meant to use these filters & some powerful examples of their use:
<&&>


Much appreciated.
If there is some way to repay the favour then rest assured I will do all I can.

Last edited by omnikron (21 Jan 2007 10:27)


The war inside the machine is as real as any war between men.
-omnikron

Offline

#2 21 Jan 2007 14:55

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,029
Website

Re: rename a file with the string given by a volume's label

Firstly basic redirection commands are explained on this page: http://ss64.com/nt/syntax-redirection.html

However the more flexible command you need to pass text output from one command into another is FOR/ F
http://ss64.com/nt/for_f.html

@echo off
setlocal
for /f "tokens=5*" %%G in ('vol^|find "drive C"') do echo drive label is [%%H] & set _label=%%H

echo %_label%

Notes

- The VOL command displays several lines of text, so the FIND command filters this down to just the one line of interest

- The pipe command | has to be escaped with ^ when used within the FOR loop.

- If you want to select variable names based on a passed parameter just use
SET %1 = whatever
but be careful the name passed is not the same as any other variables already in the script.

Offline

Board footer

Powered by FluxBB