I'm totally new to scripting, but would like to use Windows CLI compare two text files, list1.txt and list2.txt, each containing a list of values, and generate a new text file containing values that are found in one list but not in the other. I've been reading about Powershell, Shell, Batch files etc but cant seem to figure out the basics. Do I need to download anything to use these languages? Or how can I directly compare the files using the Windows CLI? Thanks.
This is very doable in PowerShell, which is preinstalled in windows 7+ and easily added in vista or less - look for some beginner powershell info on Google. You're going to want to use Get-Content and Compare-Object, then Where-Object to select which difference indicator you care about. Good luck!
To do this in the regular Windows shell, just do:
for /f "delims=" %A in (list1.txt) do #find "%A" "list2.txt" >nul2>nul || echo.%A>>list3.txt
or if you put it in a batch file, double the % signs.
for /f "delims=" %%A in (list1.txt) do #find "%%A" "list2.txt" >nul2>nul || echo.%%A>>list3.txt
This will give you all lines in list1.txt that do not exist in list2.txt, and save them into a file called list3.txt.
Related
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
I want to view all the duplicate files present in a
drive using command prompt. I have tried a few commands like tree but I am not satisfied by it.
I'll assume you are simply looking for duplicate file names, regardless of content.
This is inherently a relatively slow process. If you want a script based solution, then your best bet is probably to write a custom powershell, VBScript, or JScript script.
But I have a pair of pure script based utilities that can give decent performance. You should still expect the command to take many minutes to begin printing results (perhaps hours? if a large drive). The entire directory listing must fit within 2 GBytes. This command will fail if the limit is exceeded.
This will not allow you to see files for which you do not have access.
jren "^.*" "name()+' : '+path()" /list /j /s /p c:\ | sort | jrepl "^(.*? : ).*\n(?:\1.*\n)+" "$0" /m /i /jmatch
The above works by first using JREN to recursively list all files, one file per line as
fileName : fullFilePath
The list is then sorted, and then JREPL is used to extract consecutive lines where the leading file name repeats.
JREN.BAT is available at http://www.dostips.com/forum/viewtopic.php?f=3&t=6081
JREPL.BAT is available at http://www.dostips.com/forum/viewtopic.php?f=3&t=6044
Use JREN /? and JREPL /? to get full documentation on the utilities.
I'm need to essentially get the summary of "DIR/s" but without listing. I want to be able to see the number of files/folders/bytes used and bytes available - and pipe all that to a file?
#echo off
setlocal EnableDelayedExpansion
for /F %%a in ('dir') do (
set files=!dirs!
set dirs=%%a
)
echo %files% Files(s), %dirs% Dir(s)
Previous Batch file may be easily modified to get the number of used and available bytes, if required.
Antonio
While this can be done using command-line scripting it's FAR easier using PowerShell.
Without knowing what information you want to collect per folder and as a summary, it's not possible to provide a worked solution right now.
However, I'd encourage you to read through and experiment with the scripts and answers provided in the following threads:
A PowerShell script to find the file size and file count of a folder with millions of files?
PowerShell : how do you run a folder size summation?
powershell folder stats
HTH.
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
Many times I have found the need to copy directory names from one directory into another, creating a list of empty directories in the later.
I have achieved this task using the following command:
for /F "usebackq" %i IN (`dir /b C:/backups/sites/24/01/2012`) DO makdir C:\fabio_temp\test\%i
Now i would like to create a reusable and friendly tool so that i don't have to be typing this all the time on the command line.
Example of what I want in pseudo-language:
$dir = PROMPT('Type in the name of the directory containing the list of directories to clone:');
$dir_dest = PROMPT('Type in the destination directory:');
FOREACH LIST_DIRNAMES($dir) AS $dirname DO
MKDIR CONCAT($dir_dest,$dirname)
ENDFOREACH;
Then, it would be nice to have this function appearing in the right-click context menu. It doesn't matter what language is going to be used for this. It could be vbscript, or whatever, I don't know.
Thanks to the tutorial that #AlbertoSolano suggested, I was able to write a simple script named mkdirs_from_list.bat with the following content:
for /F "usebackq" %%i IN (`dir %1 /b`) DO mkdir %2\%%i
That does exactly what I was asking for, and, to make things easier, I'm adding the script to the path environment, so it can be invoked like this on the command line:
mkdirs_from_list C:\dir_to_list C:\dir_dest
I wanted something more friendly, like a prompt popping up, being able to browse the directories, or something alike, but I think this will suffice.