Windows Batch

12K

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

I'm looking for a GPO policy setting or a daily batch script
that could scan for dormant AD accounts & 

a) disable them if they're inactive (not logged in) for 30 days
b) delete them if they're inactive for 90 days

https://gallery.technet.microsoft.com/scriptcenter/Locate-and-Disable-4a50d304
Prefer not to use PowerShell script above as our policy (required by Audit) is
to disable PowerShell (to prevent fileless attacks).

Can consider a VB script.
0
Hello everyone,
In Windows 10. . . I need to remove the first 4 characters from about 250 file names.  They look like this: [ 005_this is a squirrel.jpg ].  I want it to read [ this is a squirrel.jpg ]

I can use CMD and I'm just starting with Powershell.

Thanks so much in advance.
elgin
1
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
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

1
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
Hello experts, some users report bad performance of application that manage my team. In order to launch an action plan I would like to report ping information in a txt file in order to discard network issues.
The idea is that user run bat or vbscript and then a txt file will be generated with ping results.
In bat & vbs file I should be able to report:
1-Adress that I want to ping
2-Time scale and frame option in order to run ping during a certain period of time.

If you have some feedback or ideas about this please don’t hesitate to help me.
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 have 100 pc's (mix of windows 10 & 7) and I need a script that will help me find out which if any need to have windows activated. I know I have at least 6 out of the 100 pc's that need reactivation. I would like the script to run against a text file of computer names.

Thanks.
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
I prefer to do this with batch, I've done lots of research and feel that I have like an 80-85% solution I just need some help to get me over the top and clean it up a bit and add some stuff I don't have working yet

batch file to compare 2 folders checking to see if files exist in both folders
if the file exists in folder A and in folder  B then copy it to an archive folder
log the file that is being copied and add the YYYYMMDD to the filename

>>>>>>>start script


@echo on

rem To add YYYYMMDD stamp to files copied to the archive folder

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set week=%%i
set day=%%j
set month=%%k
set year=%%l
)

setlocal
rem set variables for folder and network share location to compare files
set movedir=c:\move
set netdir= "\\name\folder"
set archivedir= c:\archive


rem set variable for log folder and log file
set logfile=c:\logpath\logfile.txt

rem check for the archive directory if it doesn't exist create the directory

if not exist "c:\archive" ( echo Creating Archive directory
mkdir "c:\archive"
)

rem map network path
echo Mapping Network location X:

net use x: "%netdir%"

echo Compare Move directory to a Network share

rem Check all files in folder move and Network share with a for loop ???

if the files exist in both directories then copy to archive and add a date stamp to name and log the files that were copied to the archive folder to the c:\logpath\logfile.txt

rem I believe …
0
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

Windows Batch

12K

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.