[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Windows Batch

11K

Solutions

11K

Contributors

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

Share tech news, updates, or what's on your mind.

Sign up to Post

How can i input the name of a computer and run wmic commands based on the name of the machine?
My script so far

@echo off
setlocal EnableDelayedExpansion


set /P "compname= Please enter machine name:"


wmic computersystem get name 

wmic Cpu get Name

wmic path win32_VideoController get name 

wmic ComputerSystem get TotalPhysicalMemory

WMIC LOGICALDISK where drivetype=3 get caption,size,FreeSpace

pause

Open in new window

0
Become a CompTIA Certified Healthcare IT Tech
LVL 12
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

My csv file is showing blank lines in between of each output, anyway to get rid of this? my code is below.
Thank you!

@echo off
setlocal EnableDelayedExpansion

set listfile=C:\machines.txt
set outputfile=C:\dell\%computername%.csv

(
  for /F "usebackq" %%i in ("%listfile%") do (
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i computersystem get name /format:csv 

2^>NUL') do (
      echo ComputerSystem : %%A  
  )
 for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i Cpu get Name /format:csv 2^>NUL') do ( 
echo Cpu : %%A
    )
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i path win32_VideoController get name 

/format:csv 2^>NUL') do (
      echo VideoController: %%A
    )
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i ComputerSystem get 

TotalPhysicalMemory /format:csv 2^>NUL') do (

      set TotalPhysicalMemory=%%A
      set TotalPhysicalMemory=!TotalPhysicalMemory:~0,-10!
            echo  TotalPhysicalMemory=!TotalPhysicalMemory! MB
    
    )
    echo Drives:
    for /f "skip=2 tokens=2-4 delims=," %%A in ('wmic /node:%%i LogicalDisk Where DriveType^=3 Get 

Caption^,FreeSpace^,Size /format:csv 2^>NUL') do (
      set Caption=%%A
      set Free=%%B
      set Size=%%C
      set Free=!Free:~0,-10!
      set Size=!Size:~0,-10!
     echo !Caption!, Free=!Free! MB, Size=!Size! MB
echo --------------------------------------------------------------------------------
    )
  ) 
) > "%outputfile%"

Open in new window

0
is there anyways to display logicaldisk size and free space in mb or gb?
wmic command I am currently using:
WMIC LOGICALDISK where drivetype=3 get caption,size,FreeSpace

thank you
0
I'm trying to delete files older than a day.  I'm using forfiles with the following command:  Forefiles -p "X:\directory" -s -m *.* -d 1 -c "CMD /C Del @FILE : Date -1 days"  I know I have files older than a day but it's telling me No files found with the specified search criteria.  I have files in there starting on the 13th.  What am I doing wrong?
0
what command in a batch will check for .csv files in C:\Folder\File, and if 1 or more .csv files exist, then carry on with the rest of the batch, otherwise exit.
0
Combine all .csv files into one file,
Input folder is C:\Temp1\Folder..
Output folder will be the same.
Thanks in advance.

Batch is preferred, otherwise poewrshell would be fine as well.
0
I would like to use a simple script  to delete all *.csv files found in C:\Temp1\...  except for one file called Output.csv

It would be in Batch, .ps1 or vbs..

Thanks in advance.
1
I would kind to use a script that will look at a specific folder and if there are any .csv files it should join them.
All the csv files have the exact number of positions separated by a comma
0
I would like to know the batch commands for the attached input file that will do the following.
When it sees 01-ABCDA, or 01ABCDB - in position 4 'and' if it sees either 01230c, or 01232 in position 100,
then  it will change the last character from whatever 3 digits it sees at the end of those lines, to a ,008.


This is the input script in a .csv file.

5,A,01234,01-ABCDA,Y,1251/2,1772345,,20181003,20181107,20181114,,1234567890000,COMP ACCOUNTS PAYABLE,123 ROAD WAY,,TREEWAY,FL,01234-5678,,,,1234564870004,Somewhere Warehouse,200 Treeview Court NW,,Everywhere,GA,12345-1234,,,018-441-5522,41245,ABCORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894561230,01230c,152324,STEEL,,,,123.00,US,0.00,US,0.00,US,10.5000,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101
5,A,01234,01-ABCDB,Y,1251/2,1772345,,20181003,20181107,20181114,,1234567890000,COMP ACCOUNTS PAYABLE,123 ROAR WAY,,TREEWAY,FL,01234-5678,,,,1234564870004,Somewhere Warehouse,200 Treeview Court NW,,Everywhere,GA,12345-1234,,,018-441-5522,41245,ABCORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894561560,01232,152345,WOOD,,,,789.00,US,0.00,US,0.00,US,10.5100,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,102
5,A,01234,67891,Y,1253/2,1775478,,20181003,20181107,20181114,,1234567890000,OTHERCOMP ACCOUNTS PAYABLE,123 ROAD WAY,,FOREST,TX,01234-5678,,,,1234564870004,Another Warehouse, 300 Apt View Court NW,Specific …
0
Hey Experts!  To keep a directory from filling up with backup files, I schedule this script to run once a month.  However during my testing, I noticed that the log file doesn't record what file(s) were deleted or if errors were encountered.  How do I record the outcome of the delete file action?  Thanks Experts.

cd "C:\tools\Scripts\Test"
forfiles /d -0 /c "cmd /c del /q /f @file"

Open in new window

0
CompTIA Network+
LVL 12
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

I have a batch file that is used to sftp a file by calling winscp. However if the file exist on the sftp server it does not over write it. Below is the command i am using. Can you tell me how to overwrite the file if it already exists please?
put -nopreservetime -nopermissions L:\PROGRA~1\TCI\RETAIL~1\HQPM\Batch\..\hosttran\export\Shipt\Product\Weis_product*.zip 

Open in new window

0
In batch, check if a file type exists at a specific folder  
For example, if at C:\folder\file it sees any *csv file, it should rename it to File1.csv.
0
We are trying to write a bat file that will keep two directories/folders up to date, but we only want the newest files moved i.e. so one directory has 110 files the other 100, we only want to move the 10 missing files.    At this time the bat file copies all files over, which is taking much time as the directory has many files.    Can anyone help?
0
I would like to create a batch file to use in Windows 10 Task Scheduler for the purpose of doing a daily backup of spreadsheets from one network drive folder to another network folder. I would like the batch file to include the step of creating a sub-folder in the destination 'CAP Spreadsheet Backups' folder, named with the current date (YYYY - MMM- DD), where the spreadsheets would be backed up to.

I have created the copy action:

Copy "\\ncxxx3.corp.xxxx.ca\productiongraphics$\XXX Spreadsheets\*.xls*" "\\ncxxx1\xxx_development$\Publishing\XXX\_XXX Spreadsheet Backups\

But I do not know how to add the action to create the sub-folder with the current date to the batch file.

Also, do I need to include an action in the batch file to 'End' the task?

Thanks,
Andrea
0
I would like to see a sample VBS and Batch script that will send from a gmail account using port 587, with an attachment.
0
Hi all, I need some help with a PowerShell script, I am trying to copy a folder and all contents from one location to another matching a path. Source folders to be copied are named by AD Account so: \\server1\folder\folder\ADAccountName and the destination path is \\server2\folder\folder\*\*\*\*\ADAccountName
I have got as far as matching the names but am struggling to get the needed logic
$SPath = "\\server1\folder\folder"
$DPath = "\\server2\folder\folder\*\*\*\*"
$Folders = Get-ChildItem -Path $Path -Directory |
Select-Object -Property Name,FullName
$Folders | ForEach-Object {
$TPath = "$SPath\$($_.Name)"
if ((Test-Path -Path $TPath -PathType Container) -eq $true) {
Write-Host "$($_.Name) exists" -ForegroundColor Green
}
Else {
Write-Host "$($_.Name) does not exist!" -ForegroundColor Red
}
}
0
Hi

I am in search for dos command which I need to include in the batch file. The code I am looking for is to copy excel files from a Upload folder and paste it in the archive sub folder(which sits in Upload folder). Each time I run batch file, it should copy files from Upload folder and paste in archive folder. If the files with same name exist inside archive folder, the dos command should over write only those files with latest files, leaving other files as they are.

Hope you get my point.

Any questions please let me know.

Thank you
0
Hello experts,

I have the following VBscript which allows me to restart VM's based on a config file

Const ForWriting = 2
Const ForAppending = 8
Const TriStateUseDefault = -2


Set oFS = CreateObject("Scripting.FileSystemObject")

strWarning = "Due to network maintenance, this computer must be restarted. You have 15 seconds to save your work from the start of this countdown. Sorry for any inconvenience caused. "
strDelay = 15      'Delay given in seconds; change this value to your preference, or set it to 0 to give no delay at all

'Open a text file of computer names
'with one computer name per line
WorkingDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strServerFile = WorkingDir & "\servers-mstt-prod.txt"
Set oTS = oFS.OpenTextFile(strServerFile,ForReading,True)

'go through the text file
Do Until oTS.AtEndOfStream
 'get the next computer name
 'store it in variable strCompname
strCompname = oTS.ReadLine
            WshShell.Run "C:\Windows\System32\shutdown.exe -m \\" & strCompname & " -r -c " & Chr(34) & strWarning & Chr(34) & " -t " & strDelay      'Replace the "-r" switch with "-s" to make the computers shutdown instead

WScript.Sleep 2*60*1000 '2 minutes of pause
 ' You can change time according to your need or try to use If condition with ping command
 
'WScript.Echo strCompname & " has been reboot"

Loop
'close the text file
oTS.Close

Open in new window

I have identified two issues

1-When I run the script as administrator with CMD I need  to launch it twice. First shot is not taken  into account
2-When I run the script in my computer restart is forced locally though the config file doesn't contain information related to my computer

Could you please help me with this and provide some advices concerning those issues.

Regards,
0
Good day,

I am looking for a script that will do the following.

I would like script that will look into a CSV file and check and do the following:

1.      If in position 4 it sees the number 96587, then it will look in position 28, and if it sees CT, FL, GA, IA, IN, NH, NY, WI. or PA  then change 96587 to 01-EAST,
However if in position 4 it sees the number 96587, then it will look in position 28, and if it sees  CO, CA, TX, CA or WA, then change 96587 to 01-WEST.

2.      Additionally, if it sees the following number, AC00123, AC00124, AC00125. Or AC00126, then remove the AC and add a C to the end of the numbers, for example 00123C, 00124C, 00125C, 00126C…

The content of the file looks like this:

5,A,01234,96587,Y,1251/2,1772345,,20181003,20181107,20181114,,1234567890000,COMP ACCOUNTS PAYABLE,123 ROAD WAY,,TREEWAY,FL,01234-5678,,,,1234564870004,Somewhere Warehouse,200 Treeview Court NW,,Everywhere,GA,12345-1234,,,018-441-5522,41245,ABCORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894561230,AC00123,152324,STEEL,,,,123.00,US,0.00,US,0.00,US,10.5000,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
5,A,01234,96587,Y,1251/2,1772345,,20181003,20181107,20181114,,1234567890000,COMP ACCOUNTS PAYABLE,123 ROAR WAY,,TREEWAY,FL,01234-5678,,,,1234564870004,Somewhere Warehouse,200 Treeview Court …
0
Rowby Goren Makes an Impact on Screen and Online
LVL 12
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Hi,

I am searching for a dos command which can cut files from a given folder and paste those files in to an Archive folder which sits along side with the files need to be uploaded into. There are folders with space in its name. I have to insert this command in a bat file. I use below code for my other tasks and its working fine, but it does not do cut and paste, it does copy and paste.

@echo off
setlocal

echo Start Copy

set SOURCE_DIR=C:\Nat\Reports\Reports
set DEST_DIRS="X:\", "N:\Reports\Production Repts\Reports"
set FILENAMES_TO_COPY="2018All Projects_New.xlsx","2017Report_New.xlsx"

pushd "%SOURCE_DIR%"

for %%F in (%FILENAMES_TO_COPY%) do (

    for %%D in (%DEST_DIRS%) do (
        echo file "%%~F" to "%%~D"
        xcopy /Y "%%~F" "%%~D"
    )

)

popd

echo. Done.
exit /b

Thank you
A
0
CN.txt - List of computernames
services.txt - list of services to check

@echo off
 for /f %%a in (CN.txt) do (
   echo %%a >> Output.txt
   for /f "tokens=*" %%b in (services.txt) do (
     sc \\%%a query "%%~b" >> Output.txt
   )
 )
 pause

How do I clean this script up to only show the Service_Name and state?
0
How do I add a computername to the output file?

CN.txt - List of computernames
services.txt - list of services to check

@echo off
for /f %%a in (CN.txt) do (
  for /f "tokens=*" %%b in (services.txt) do (
    sc \\%%a query "%%~b" >> output.txt
  )
)
pause

The current output only shows the check of the service but not a computername
0
Hello all,

When we push updates, users are not getting option to suppress the reboot. Is there any command line in powershell or cmd to force stop the restart.

we tried shutdown /a it didnt work.. is there any other options?

regards
DJ
0
Powershell/VBS/Batch script to show the last 30 days, and to indicate which day is today.. in a calendar format
1
Hello experts,

I have a root folders which contains multiple folders.

Root folder: C:\Root
Extension file : mdmp

I am looking for a cmd command to remove files with specific extension. Removal process should be performed with drill down.

If you have questions, please contact me
0

Windows Batch

11K

Solutions

11K

Contributors

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.