[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

I created a batch file with the following command for test (before I set in production):

TestMe.bat
REM echo off
for /L %%A IN (1,1,10000) do echo %%A 2>&1 | wtee -a outputv2.log & for %%F in ("outputv2.log") do if %%~zF GTR 500 del %%F | echo %date% %time%  File %%F with size %%~zF >> deleted.log

Open in new window


Question:
Why is the date and time not changing after deleting "outputv2.log" and logging in "deleted.log"? Where is the bug or how can I correct it?

For me it seems that on every delete it logs the starting  date and time of the batch file.

deleted.log:
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
09.11.2018 21:23:45,17  File "outputv2.log" with size 504
0
Build an E-Commerce Site with Angular 5
LVL 12
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

The zip file is a folder containing 6 files.

Three originals and three examples of the required result.

In each of the original files:
la_162.csv
la_163.csv
la_164.csv

column A contains a heading in A1  and a list of numbers from A2

The requirement is to be able to highlight all original files in the folder and run a script that
1. includes the number from the file name in column B as per the example for each file and
2. renames the amended file to include the heading from Column A in that file name.
EE-folder.rar
0
Is it any example to write a shutdown script such that it will randomly shutdown the Window after 0:00, close the Outlook if it is opened and copy the ost file to the server ?

Thx
0
I am having trouble with my batch files .bat.  The folder names have spaces in them and the commands do not like it.

Powershell also has this problem.

There are many files to process so renaming them manually is not an option.

How can I get around this?
1
Hello experts,

I have the following layout

Root Folder —- with files and subfolders

I need to use robocopy to only copy the files in the root folder and skip any subfolders. Just going after the files at the root level folder but not the subfolders.

Ie

Root folder — has subfolders 1,2,3 and files a,b,c, etc

I only want to copy files a,b,c and any other files but not the subfolders.

Any help?
1
is there a way to add to the windows credential manager via the command prompt
I'm not referring to cmdkey but the actual windows credential manager apparently they are not the same thing.
When I use cmdkey /generic:TERMSRV/server /user:username /pass:password
it works fine it I run mstsc /v:server  
but if I run it from the desktop by clicking on a shortcut it prompts me for the password; however, if I put the credentials in windows credential manager I do not get the prompt

so, is there a a command to add a username and password to windows credential manager ?
I don't want any user interaction I want to run it through a bat script or psexec or something.
Thanks!
0
Batch scripting

Experts,

I have run across an old dos batch script that utilizes something I have never seen.

The script is used to copy a file, but the reasons behind and the uses for the [ ] is what I am looking for.

copy h:filename.txt i:[Spreadsheet]\folder1\folder2

Open in new window

1
Is there any way to list all the folders and files under a particular directory ?

Thx
0
I would like a simple script in .bat, .vbs, or powershell, which will look into a .txt fle and do one of the following, whichever is easier to do:

1.  Whenever it sees specific text such as AD, or DE, or BOG, or STOW..  that it will insert a tilde character (~) just before the text and then save the file with a new name.

OR

2.  Whenever it sees the specific text such as AD, DE, BOG, or STOW, that it would start a new line which starts with those characters, and only start another line when it finds the next set of specific text.

The file resides at:  
C:\Users\user\Desktop\folder\file.txt
And the output should be in the same folder but renamed to:
C:\Users\user\Desktop\folder\fileR.txt
2
Hello experts,

I use the following script the script reported at:
https://www.experts-exchange.com/questions/29123510

which allows me to ping based on multiple loops

I would like add in the script traceroute operation in order to generate ping logs and traceroute based on IP’s reported in config txt file.

Adaptations to include :
-Include traceroute operation in the script
-Change log output name in order to include log-ping_IP_YYYYMMDDMMHHSS, log-traceroute_IP_YYYYMMDDMMHHSS
-Loop parameter will cover ping operation and traceroute operation.

If you have questions, please contact me.
0
Fundamentals of JavaScript
LVL 12
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Hello experts,

I am looking for a script in order to get the process view of task manager in a csv file.

2018-10-30_11h04_42.png
The various values should be displayed as they are displayed in taskmanager: CPU %, Memory MB etc;;.

If you have already a script that output automatically this type of information could you please share with me?

Thank you again for your help.
0
Hello experts,

I have use the following command to get system information and process list
Processlist
Systeminfo
I would like to extract information from a user, as a result I was wondering if you can help to set up a bat file to output both informations in differents files.
Both output files should generated in the same location as the bat file.

If you have questions, please contact me.
0
Windows Batch file to count a set "N" number of files from a single source folder,merged multiple files together into a single output file that contains <= N number of records, and move processed files to a save directory folder.

Request for a window bat file that can be run from command/CMD prompt.  Windows 7 environment.

Windows Batch file to copy a set "N" number of files from a single source folder to an output file, move processed records to a single save folder.  The single source directory may contain 1 up to a million files.  
Need to copy 200 files at a time to a newfile_N+ that should contain <= 200 files per newfile_N+ until all records have been read and processed.

Input file extension: *.hl7
Output file newfile_N+ extension: *.txt

Source directory - C:\temp\source
Destination directory - C:\temp\output\newfile_N+.txt
Destination directory - C:\temp\processed

For the purpose of demonstration I have attached (5) sample hl7 files.
Each newfile_N+.txt file should contain <= 2 records per file.

Prior to kicking off the batch file.  All (5) source files would be all be in the (C:\temp\source) directory.
Upon completion of the batch program running.  The expected output once the bat file has run would result in the following:

C:\temp\source
      dir: contains no files *.hl7 files
      
C:\temp\output
 dir: newfile.txt, newfile1.txt, newfile2.txt
      Note: Each of these files contains <=2 records each, max is 2 records per file
      ==> newfile.txt …
1
Hello experts,

I have the following batch file, reported at:
https://superuser.com/questions/309403/batch-file-to-write-ping-results-to-a-text-file
@echo off

set/p host=host Address: 
set logfile=Log_%host%.log

echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)

Open in new window


I would like to add some enhancement
-Be able to report the various ip in a config-file.txt
-Be able to output multiple files related to ip reported.
-Parse the time of ping for example I would just to ping during 2 minutes and then exit.


If you have questions, please contact me.

Regards,
0
I would like to join two as many text files that are found in C:\Files\.... into one file ending in .txt
with no lines that it finds in between the data in one text file and other etc..
0
Batch script to check for IP address and install if errorlevel==0 but exit if errorlevel==2 not working.

The script below is to determine if an end user is connected via vpn.  

When connected to VPN ipconfig | find returns "2"
When not connected it returns "0"

The script works when I am not connected to VPN.  When I'm connected to VPN, I run ipconfig | find manually and I get "2" but the script just flashes and does nothing.  I can't figure out what I've done wrong in this script.

@echo off

REM Check to see if an IP address in the VPN range is present.
REM Install Cisco AC if no VPN IP address is present.
REM Exit withoug install if VPN address is present

ipconfig | find /c "168.32." >NUL 2>NUL
 if "%errorlevel%"=="2" goto VPN
 if "%errorlevel%"=="0" goto Install

Install:

echo.
echo This is install!
Pause
goto End

VPN:

echo.
echo SCCM is attempting to install Cisco AnyConnect (your VPN) over a VPN connection!
echo.
echo This will cause a disconnect and possible loss of work.
echo.
echo You can attempt again when you are on site.
echo.
Pause



End:

Exit /b 0
1
Request for a window batch file that can be run from command/CMD prompt.  Windows 7 environment.

Windows Batch file to move a set "N" number of files from a single source folder to a destination folder.  The single source directory may contain 1 up to a million files.  Need to copy 200 files at a time to a destination folder_N+ that should contain <= 200 files per destination directory.

Once all the files are moved to the destination folders, would like to merge the multiple files per individual destination folder into one single file. (copy *.hl7 newfile.hl7)

File extension: *.hl7
Source directory - C:\temp\source
Destination directory - C:\temp\outputN+

For the purpose of demonstration I have attached (5) sample hl7 files.  
These (5) files would be all be in the (C:\temp\source) directory.  
Expected output would result in the following:

C:\temp\output
 dir: new 1.hl7, new 2.hl7
 copy *.hl7 newfile.hl7

C:\temp\output1
 dir: new 3.hl7, new 4.hl7
 copy *.hl7 newfile.hl7

C:\temp\output2
 dir: new 5.hl7
 copy *.hl7 newfile.hl7

Note: The folder names may contain spaces, also the file names may contain spaces.
source.zip
0
I would like to search a folder, for example C:\Attachement\Inbox..  for any files that start with the  following  INBOUND123..... .in  
then I could like to copy if found to another folder called C:\Attachments\Transferred..

What batch command can I use to do this?
0
Hi,
I have the following piece of code in a batch file that I do not understand
::    
::                                         * Clear Global ErrorLevel Env Variable
    SET ERRORLEVEL=
::
    verify >nul
::
:BEGIN
::                                         * Start of Main Processing

Open in new window

My interpretation is
SET ERRORLEVEL=
sets the ERRORLEVEL variable to an empty "null" state.
 I am told the
verify >nul statement
means
It means we are emptying the value of ERRORLEVEL before checking for success or failure of the current code, just to ensure it does not hold any previous values.

however when I run the following on my local windows 7 workstation
batch outputI do not see that the variable is "empty" , it is consistently zero.
I am expecting the second ERRORLEVEL output to be "empty".
Also I do not understand the verify >nul code...
does verify >nul , set the variable to NULL? Is that what it is meant to do?
Thanks
0
Python 3 Fundamentals
LVL 12
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Does GPO / AD has feature or policy that could disable accounts that are inactive
for a certain number of days?

There are PowerShell scripts around but we are told to disable/remove Powershell
due to fileless attacks.

Ideally a windows batch or VB script.

I noticed  ' net user /domain  userid | find "Last logon"  '  has a date : if we could
iterate through all domain IDs & calculate based on this date, it will help.

There's oldcmp tool which seems to work for Win 2012 R2 AD but the csv output
it gives doesn't seem to provide any domain id in it or I've used it wrongly?
1
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
Using Windows batch how can I rename up to 4 files, 1.pdf, 2.pdf, 3.pdf and 4.pdf all based on time stamp, 1.pdf being the file with the earliest time stamp..

At C:\Attachment\.... there could be several files all names differently, but all. pdf files.  
I want to rename each file based on when they were created etc..
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

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.