Renaming a computer against CSV from a batch file - windows

We're doing a mass rename of computers at a company and while I know there is a way to do this mass rename via Powershell from a DC, there are a lot of computers that are turned off and the script will need to be run again or stragglers will need to be individually fixed.
We have a remote monitoring/management system for this company that can deploy .bat files to computers as soon as they turn on, so I figured this way would be more thorough, and also has less chance of screwing things up seeing as it's a computer renaming itself as opposed to one script renaming everything.
I came across this, and it seemed like a really good idea, but for some reason the string for "%name%" can't be found in my semicolon delimited text file.
Here's what I think the script is / should be doing:
The script finds the name of the computer that it's being run on, sets it as %oldname% variable. Then it searches the semicolon delimited text file. This file contains current names and desired new names of computers. The two names are delimited by semicolon.
So the script tries to find the previously found name, then move over to the second part of the line to find the new name (tokens2 delims=;).
Then it loads that into the newname variable, that way the wmic command can run using oldname and newname.
However, the batch file cannot find the string.
for /f %%i in ('hostname') do; set oldname=%%i
for /f "tokens=2 delims=;" %%i in ('type \\server\shared\test\names.txt ^| findstr /i "%name%"') do; set newname=%%i
wmic computername where name="%oldname%" call rename name="%newname%"
I get "Findstr: No search strings" This tells me that it cannot properly find one of the names in the text file, and I'm a bit lost. I don't know if it has to do with syntax or how the txt file is set up.
The text file is laid out as such (using three fake lines to demonstrate formatting):
Test-L7;New-3284
Kitchen-7;New-1249
MikeS-7;New-3290

Related

writing a batch file for loop to rename and move files

I've never used Windows cmd scripting before; I'm trying to write a batch script, What I need to do:
I have a lot of folders, named numerically. Each one contains a file. All the files have the same name.
e.g.
folder1\file folder2\file
I want to rename and move the files, so they are named numerically and in the one folder
e.g.
newfolder\file1 newfolder\file2
My script for two test folders is:
FOR /L %%A IN (1,1,2) DO
(
move "folder%%A\file.txt" "newfolder\file%%A.txt"
)
I suspect this is all wrong. I get "the syntax of the command is incorrect".
Just move the opening parenthesis on the first line:
FOR /L %%A IN (1,1,2) DO (
move "folder%%A\file.txt" "newfolder\file%%A.txt"
)
Newlines aren't as invisible to the batch interpreter as in most other languages, meaning you have to explicitly tell it to look on the following lines.

Convert list of paths to list of filenames

Background
I find myself often copying file paths to the clipboard, which is somewhat cumbersome to do from Windows Explorer.
So I wrote a little .bat file to put into the %APPDATA%\Microsoft\Windows\SendTo\ folder utilising the CLIP executable to copy a list of the selected file paths to the clipboard. This file consists only of a single line:
echo|set /p= "%*" | clip.exe
Which works quite nicely, I can select one or more filenames in Explorer, right-click on them and "Send To" the .bat file, which copies them to the clipboard. Each file path is complete and separated from the others by a space character.
Question
Sometimes, I don't want to copy a list of the full file paths, but would prefer to have a list of just the filenames with their extensions. I know how to do that conversion for single file paths, using the %~nx syntax as described here or here.
I tried different combinations of these but can't seem to find a workable solution for my list of paths. The following code echos the filenames correctly:
for %%F in (%*) do echo %%~nxF
...but how do I combine them to pass through to CLIP? Do I have to do string concatenation? Maybe in a subroutine to be called, or is there a more elegant solution?
The following will put each file name on a separate line within the clipboard:
#(for %%F in (%*) do #echo %%~nxF)|clip
If you prefer, the following will put a space delimited list of file names on a single line, with quotes around each file name.
#(for %%F in (%*) do #<nul set /p =""%%~nxF" ")|clip
Couldn't you just:
echo|set /p= "%~nx*" | clip.exe

Win 7: CMD batch file for creating directories based on filenames

I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.

Batch File/Powershell - enumerating files in a folder

Note: There is no need to use batch per say, but I am just familiar with batch, Powershell would be better I imagine, so if there are easier solutions for this problem in powershell, please shout!
I have the arduous task of testing our DR backups for all our clients, that is, mounting ShadowProtect Snapshots latest incremental, writing and reading a file, them unmounting the image. The actual ShadowProtect part of batch is fairly simple but I would like to design a batch that can automate this.
Essentially my question is:
How in a batch file can I firstly, enumerate files in a folder, and then place a specific part of a given filename into a variable?
Reason being ShadowProtect incrementals have a naming convention such like:
SERVERNAME_DRIVELETTER_b00X_i000x - whereby b = base image, i = incremental number
I need to mount the latest incremental image, therefore need to parse the folder and find the latest incremental image, based on the number following the i in the filename.
Is this possible in batch?
Thanks!
Something like this should work:
#echo off
setlocal EnableDelayedExpansion
for /f "delims=_ tokens=1-4" %%f in ('dir /b *_*_*_*') do (
set servername=%%f
set driveletter=%%g
set base_image=%%h
set increment=%%i
)
echo !servername!
echo !driveletter!
echo !base_image!
echo !increment!
endlocal
If you have several matching files and want to do something with all of them, you need to put the processing code inside the loop.
Edit:
for /f: process either a file or the output of a command enclosed in single quotes
delims=_: fields in the processed content are separated by underscores
tokens=1-4: assign the first four tokens to the parameters %%f through %%i (first parameter is the one given in the for statement)
dir /b *_*_*_*: list all file where the name contains at least 3 underscores with just their file name (the output of this command is processed by the for loop)
setlocal EnableDelayedExpansion: expand variables at run time (otherwise assigning the parameters to variables wouldn't work)
For further details see help for and help dir.
You could always use vbscript or jscript. It is much more powerful than batch files. Also jscript and vbscript hosts are available also on machines that don't have powershell!
Link for enumeration:
http://www.techimo.com/forum/webmastering-programming/100453-recursive-javascript-list-all-files-folders-given-folder.html
Jscript string reference:
http://msdn.microsoft.com/en-us/library/bxsyt3yc(v=vs.80).aspx
You should be able to combine the two.
you run your jscript (I prefer jscript to vbscript because of its resemblance to javascript)
cscript scriptname

Looking for a simple Batch script that modifies file name

I have a list of files in a folder that end with .swf.
I want to change all those files from X.swf to X<some number>.swf.
How can I do that?
This little script will change all *.swf files into the equivalent *_42.swf files.
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f %%a in ('dir /b *.swf') do (
set fspec=%%a
set newfspec=!fspec:~0,-4!_42.swf
echo ren !fspec! !newfspec!
)
endlocal
Actually, as it stands now, it will just echo the commands that it wants to execute. Once you're happy they're correct, you can just remove the echo from that renaming line above.
It works by using for /f to get a list of all SWF files and then using string manipulation to:
remove the last four characters (the.swf extension); then
add a new _42.swf extension onto the end.
And, please, make sure you back them up first :-)
You could use the following one-liner directly from the command prompt:
FOR %F IN (*.swf) DO RENAME "%F" "%~nF123.*"
where 123 stands for your number of choice.
Alternatively you could create a batch file and take advantage of its ability to accept parameters. Use the following script:
#ECHO OFF
SET "suffix=%~1"
FOR %%F IN (*.swf) DO RENAME "%%F" "%%~nF%suffix%.*"
Now if the batch's name is renamer.bat, you can invoke it like this:
renamer.bat 2011
and it will add 2011 to the name of every .swf file in the current directory.
Assuming <X> in your description is supposed to be constant and you don't explicitly require a batch script to solve your problem, you can use Windows Explorer as mentioned in an article by Microsoft titled "Rename a file".
Here's a an extract from said article:
"You can also rename several files at one time, which is useful for grouping related items. To do this, select the files [then press F2]. Type one name, and then each of the files will be saved with the new name and a different sequential number at the end (for example, Renamed File (2), Renamed File (3), and so on)."

Resources