Data Consolidation using Batch Script - consolidation

I have following string in text files:
Hostname: ABCD
Manufacturer: LENOVO
Model: 0585AL7
Domain: XYZ.COM
Is Part of Domain: True
Current Logged In User: IN\USER
Number of Processors: 1
Operating System: Microsoft Windows 7 Enterprise
Name: Microsoft Windows 7 Enterprise |C:\Windows|\Device\Harddisk0\Partition1
Organization: XYZ
RegisteredUser: XYZ
SerialNumber: 00392-918-5000002-85350
WindowsDirectory: C:\Windows
Version: 6.1.7600
Original Install Date: 20110319025551.000000+330
I am using below command to parse the content.
FINDSTR /B /S /I "Operating System" *.txt >> OsMatch.lst
FINDSTR /B /S /I "Version" *.txt >> OsMatch.lst
FINDSTR /B /S /I "Manufacturer" *.txt >> OsMatch.lst
FINDSTR /B /S /I "Domain" *.txt >> OsMatch.lst
FINDSTR /B /S /I /C:"Is Part of Domain" *.txt >> OsMatch.lst
FINDSTR /B /S /I /C:"Current Logged In User" *.txt >> OsMatch.lst
FINDSTR /B /S /I /C:"Number of Processors" *.txt >> OsMatch.lst
FINDSTR /B /S /I "RegisteredUser" *.txt >> OsMatch.lst
FINDSTR /B /S /I "Organization" *.txt >> OsMatch.lst
FINDSTR /B /S /I "SerialNumber" *.txt >> OsMatch.lst
FINDSTR /B /S /I "WindowsDirectory" *.txt >> OsMatch.lst
FINDSTR /B /S /I /C:"Original Install Date" *.txt >> OsMatch.lst
ECHO Hostname,Operating System, Version, Manufacturer, Domain, IsPartofDomain, CurrentLoggedInUser, No of Processor,
RegisteredUser, Organization, Serial Number, WindowsDirectory, InstallDate, Location > Desktop-OS.csv
FOR /f "tokens=1,2* delims=:" %%a in (OsMatch.lst) do ( set new1=%%a
SET new2=%%c
SET new1=!new1:~0,-4!
FOR /f "tokens=1,* delims=\" %%a in ('echo !new1!') do (
SET new5=%%a
SET new6=%%b
FOR /f "tokens=* delims= " %%a in ('echo !new2!') do (
SET new4=%%a
)
)
IF {!new6!}=={} (echo !new5!,!new4!,NA >> Desktop-OS.csv
) else ( echo !new6!,!new4!,!new5! >> Desktop-OS.csv)
)
Output is coming in excel file, but in each row, I want output under every column as below
Hostname Operating System Version
ABCD Microsoft Windows 7 Enterprise 6.1.7600
likewise all the TABS
"Manufacturer Domain IsPartofDomain CurrentLoggedInUser No of Processor RegisteredUser Organization Serial Number WindowsDirectory InstallDate Location"
.
Thanks in advance.. :)

Related

.bat to search for directory with certain name and set into a copy path

I'm trying to search for a folder (c:\test) in a certain directory that has the word "current" in it. And then i would like to copy from a folder inside it (c:\test\current\first).
Any help would be much appreciated. I have done my research but so far I have only managed to do xcopy, but not the first 2. Sorry I'm relatively new to this.
I am not sure of the structure, but seems you want to do:
from Batch file:
For /f "delims=" %%i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do echo %%i
From Cmdline (Console):
For /f "delims=" %i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do echo %i
So as per your last comment, this is more or less what you would need to perform the xcopy:
For /f "delims=" %%i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do (
xcopy "%%i"* /D /C /Q /R /Y /I /S "D:\Abc" & goto :eof
)

forfiles command ignore a directory

I want to write a batch that finds all docs less than 50 mb in c:\ and copy them in a folder but ignore system directory docs. I prefer it does not even search in the system dir.
Here is my batch that finds and copies all files less 50 mb in right directory but i can not make it to ignore system from searching or C:\Windows directory.
#ECHO off
:: variables
SET odrive=%odrive:~0,2%
SET backupcmd=xcopy /s /c /d /e /h /i /r /y
MKDIR "C:\Users\Documents\USBBackups\DOC\C"
forfiles /P C:\ /M *.DOC* /S /C "cmd /c if #fsize leq 50000000 echo #PATH " > "C:\Users\Documents\USBBackups\DOCC.txt"
FOR /F "tokens=*" %%a in (C:\Users\Documents\USBBackups\DOCC.txt) do xcopy %%a "C:\Users\Documents\USBBackups\DOC\C" /c /h /i /r /y
#ECHO off
There is no way to tell forfiles to exclude certain directories when switch /S is provided. You will have to write your own code that does that.
I would not use forfiles for that due to poor performance, but standard for instead:
#echo off
for /D %%D in ("%SystemDrive%\*.*") do (
if /I not "%%D"=="%SystemRoot%" (
pushd "%%D"
for /R %%F in ("*.doc?") do (
if %%~zF LEQ 50000000 (
echo %%F
)
)
popd
)
)
Here the root directory level is enumerated by for /D. All directories other than %SystemRoot% are enumerated recursively by for /R.
I changed the search pattern from *.doc* to *.doc? in order not to include files ending in .doc.lnk for example, which I guess you do not want to be retrieved.
Instead of the echo command you can directly place your xcopy command line with "%%F" provided as the copy source.
You can do the same directly in command prompt as a one-liner, like this:
for /D %D in ("%SystemDrive%\*.*") do #if /I not "%D"=="%SystemRoot%" pushd "%D" & (for /R %F in ("*.doc?") do #if %~zF LEQ 50000000 echo %F) & popd
I recommend not to walk through the entire directory tree and later filtering by something like findstr /V /I /L /B /C:"%SystemRoot%", because in that case you were wasting time enumerating a huge number of items which you ignore afterwards.
However, if you do want to rely on forfiles /S, the working command line looks like this:
2> nul forfiles /S /P "C:\\" /M "*.doc*" /C "cmd /C if #isdir==FALSE if #fsize LEQ 50000000 echo #path" | findstr /V /I /L /B /C:"\"%SystemRoot%"
Adapt this technique of using findstr to filter out certain names.
To see size of folders in Documents, excluding music, video, or pictures folders.
for /f "skip=3 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Personal"') do set doc=%A
for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"^|findstr /l /v "Pictures Music Video"`) DO #echo %j&echo.
However you could start the forfiles command in the c:\users or the particular users home folder (%userprofile%). You specify to start at c:\ which includes all folders.
forfiles /P %userprofile% /M .DOC /S /C "cmd /c if #fsize leq 50000000 echo #PATH "
forfiles /P c:\users /M .DOC /S /C "cmd /c if #fsize leq 50000000 echo #PATH "

How to rename multiple folders that have a set number of files within that folder using batch script?

I am very new to programming so your help is much appreciated!
I have a top directory, containing subfolders which have files in them. I want to add the string "x" after the folder name e.g. foldername -> foldernamex if the subfolders contain less than 4 or greater than 4 files. How do I go about doing this?
Here is my attempt so far:
for %%e in ('dir ^| find "File(s)"') do (
set cnt=%%e echo File count = %cnt%
if File count <4 do ren "%%e" "%%~nxf_x"
)
This uses the dir command and the brief /b switch with the file-only switch /a-d and pipes the result through the find /c /v "" command to count the number of files.
findstr is used to find the exact numeral 4 and if it doesn't detect 4 then the script renames the folder and restarts the loop to handle nested subdirectories (because the folder structure changes when a folder is renamed).
It's not tested - so test it on a sample folder tree with copies of some folders first.
#echo off
:loop
for /f "delims=" %%a in ('dir /b /s /ad ^|findstr /v /i "x$" ') do (
dir "%%a" /b /a-d 2>nul |find /c /v "" |findstr "^4$" >nul || (ren "%%a" "%%~nxax" & goto :loop)
)
pause
Simpler code for processing only the folders inside the current folder.
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do (
dir "%%a" /b /a-d 2>nul |find /c /v "" |findstr "^4$" >nul || ren "%%a" "%%~nxax"
)
pause

Want to exclude specific files with dir findstr

I want to find all *.csv files within a folder-structure except 2 files
This is my code
for /f "tokens=*" %%i IN ('dir /b /s *.csv | findstr /v /i "\combinedold.csv" | findstr /v /i "\combined.csv"')
The 2 files are "combined.csv" and "combinedold.csv".
The basic command should be something like
dir /s /b /a-d *.csv | findstr /v /i /e /c:"\\combinedold.csv" /c:"\\combined.csv"
Now, to include it inside a for /f command, it is necessary to escape non quoted special characters (the pipe in this case), so it becomes
for /f "delims=" %%i in ('
dir /s /b /a-d *.csv ^| findstr /v /i /e /c:"\\combinedold.csv" /c:"\\combined.csv"
') do echo %%i

How to list all the empty directories using windows batch file?

I want to create a windows batch file which lists all the empty sub-directories present under the user specified root directory.
Can anybody help regarding the same?
#echo off
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)
The above solution ignores Hidden folders. I've also been told that using both /D and /R options with FOR is bugged, though I've never had a problem with it.
#echo off
dir /a /b %1 2>nul | findstr "^" >nul || echo %%~fA
for /f "eol=: delims=" %%A in ('dir /s /ad /b %1') do (
dir /a /b "%%~fA" 2>nul | findstr "^" >nul || echo %%~fA
)
The 2nd solution that avoids FOR /D /R will include Hidden folders. But I believe it can fail if folder names contain Unicode.

Resources