I recently took interest in batch file coding and now i have a project i am working on.
in the project, i have created a basic program that require users to input their Licence code which i have stored in a text file(about 50 lines of licenses) and embeded it in my batch file with a third pary application that convert my batch file to .exe(about 50 lines of licenses). i have about 42 computers in my organization branch where i want to deploy the program.
My challenge however is that since the batch file and the Licence file are all in one .exe, One key would work on all 42 computers. which is very bad.
i have tried using wmic csproduct get uuid to compare computers but i just could not find my way.
Please i really need help with this.
here are my work so far:
SETLOCAL
:startup
SET "Password_File=%UserProfile%\Desktop\Password_File.txt"
set "fail="
set "Activation_Code="
set /p "Activation_Code=Your Activation Code Here: "
REM cls
IF NOT DEFINED Activation_Code ( GOTO :startup
)
findstr /X /l /c:"%Activation_Code%" <"%Password_File%" >nul || set fail=1
if defined fail (
echo Invalid Activation Code!
"%UserProfile%\Desktop\Msgbox Failed.vbs"
GOTO :startup
)
:yes
REM cls
echo Success, you have access!
REm >> %Password_File% echo %Activation_Code%
"%UserProfile%\Desktop\MsgboxSuccess.vbs"
"C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe"
:: This line remove the already entered activation code from the licence file copied to the computer
type %Password_File% | find /V "%Activation_Code%" >XXFile.txt
del "%UserProfile%\Desktop\Password_File.txt"
ren %UserProfile%\Desktop\XXFile.txt Password_File.txt
goto :eof
:nope
REM cls
echo Activation Code has already been used or Invalid!
"%UserProfile%\Desktop\Msgbox Failed.vbs"
:: ping locahost -n 3 >nul
GOTO :startup
i want my batch file to log the following to a destkop file
where LOG_FILE = %Userprofile%\Desktop\LogFolder\LOG_FILE.txt
wmic csproduct get name >>%LOG_FILE%
wmic csproduct get UUID>>%LOG_FILE%
then compare the UUID log in %LOG_FILE is the same as the current computer's UUID
if so then it should verify the same for the Computer name.
the whole idea
#echo off
if wmic csproduct get UUID == "DAC2D186-1AB9-E111-A120-B888E34233CEE" (echo Done! ) else ( echo Not Done!) pause
The future as stated by Microsoft is PowerShell and CIM. I have not yet found a CIM class that includes UUID, but this will work.
FOR /F %%u IN ('powershell -NoLogo -NoProfile -Command "(Get-CimInstance -ClassName Win32_ComputerSystemProduct).UUID"') DO (SET "UUID=%%u")
ECHO UUID is set to %UUID%
use a for /f loop to get the output of a command:
for /f "delims=" %%a in ('wmic csproduct get UUID^, name /format:list ^|find "="') do set "%%a"
echo "%uuid%"
echo "%name%"
Related
I want a batch script to search for software's on a computer by a specific vendor and save the results to a CSV file.... I am tried it with the following script, but it always returns NULL values, not able to figure out the issue.
#echo off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
:Sub
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%Microsoft%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
exit
NB : % is needed to be used as wildcard
In this case you should add another percent character % to %Microsfot% to escape the wilcard of WMIC in your batch and become like %%Microsoft%%
#echo off
Title Batch Script to Search for Softwares by Vendor to a CSV file
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
:Sub
echo(
echo( Please wait a while ... Searching for installed softwares ....
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%%Microsoft%%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
Exit
I tried to write a batch script that find all the paths of files that have the same name as the input string. right now it can find only the first file found, and i cant think of a way to make it list multiple files locations. I am not very experienced and I need some help.
this is part of the script code:
:start
cls
echo Enter file name with extension:
set /p filename=
echo Searching...
for %%a in (C D E F G H U W) do (
for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
set file=%%~nxb
set folderpath=%%~dpb\
::the path of the file without the filename included "C:\folder\folder\"
set fullpath=%%b
::the path of the file with the filename included "C:\folder\folder\file"
goto break
)
)
:notfound
cls
echo Enter file name with extension:
echo %filename%
echo File Not Found!
ping localhost -n 4 >nul
goto start
:break
if "%folderpath:~-1%"=="\" set folderpath=%folderpath:~,-1%
cls
echo ? %filename% found
echo %fullpath1%
echo %fullpath2%
echo %fullpath3%
--- || ---
I want the script to search the computer and list every encountered files with the same name and I want to be able to put those files' paths into different variables.
For example, if readme.txt is the input, then I want the list of all the paths of all the files with that specific name (readme.txt) and I want to set variable for each path so I can use it after that.
example:
input:
readme.txt
output:
3 files found
C:\folder\folder\readme.txt
C:\folder\folder\folder\readme.txt
D:\folder\readme.txt
variables:
path1 = C:\folder\folder\readme.txt
path2 = C:\folder\folder\folder\readme.txt
path3 = D:\folder\readme.txt
Here is a single script which will performs the task requested. It searches all drives for your input filename with extension, puts each found full file path into a variable, then output those variables for you. I will not be tailoring the script for any new changes after this response, advising on how to integrate it into the rest of your script or supporting any changes you may subsequently make.
#Echo Off
Set/P "SearchTerm=Enter file name with extension: "
Set "i=0"
For /F "Skip=1" %%A In ('WMIC LogicalDisk Where^
"DriveType>1 And DriveType!=5 And FreeSpace Is Not Null" Get DeviceID'
) Do For %%B In (%%A) Do (For /F "Delims=" %%C In (
'Where/F /R %%B\ "%SearchTerm%"2^>Nul') Do (Set/A i+=1
Call Set FullPath[%%i%%]=%%C))
If %i% LSS 1 (Echo= %SearchTerm% Not Found) Else (
Echo= %SearchTerm% had %i% match(es^)
For /L %%A In (1,1,%i%) Do (
Call Echo= %%%%FullPath[%%A]%%%% = %%FullPath[%%A]%%))
Timeout -1 1>Nul
Please be warned that searching many locations and putting many files into variables may over burden your system and cause issues.
I'm trying to use the same script "prnmngr.vbs" from Microsoft to redirect the default printer to the LP3 port. I guess this is the most effective and standard way to detect default printers that I found so far but no success so far. I would like to keep it really simple using a .cmd file.
The code below will map the default printer using "WMIC" but it will not redirect to any LPT:
#echo off
FOR /F "tokens=2* delims==" %%A in (
'wmic printer where "default=True" get name /value'
) do SET DefaultPrinter=%%A
exit
My goal as I said is to Redirect the unknown default printer to the LP3 on the unknown computer and it will be great if the result from prnmngr.vbs can be parsed somehow in order to map the Default Printer to the LP3 port. Or to modify the prnmngr.vbs to do the usual command :
net use lpt3 \\computername\sharedprinter
So far I just can't handle the whole VBS coding to do what I need. I got confused with all the functions inside.
The command below will map the printer without any problems only if you have both \Servername and \Sharedprinter but on my case they will be unknown.
Cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -ac -p "\\servername\sharedprinter"
The Default printer could be a shared network printer or a USB/WiFi and obviously the computer name will also be unknown. I can't check one by one between 5000 machines or users to search and get their devices to give them a command to map their printers on their own every time they execute their application!
I just been told that the name "Imprimant" is the name all the Local Printers shared name will be used so as long as this name doesn't change at all it should be fine.
But at the moment this script is working only for the 1st case (:Shared) and the Local case is not working at all. If I invert the ERRORLEVEL it will work the (:Local) and Not the (:Shared).
But so far I got a new scrip and it goes like this:
#echo off
FOR /F "tokens=2* delims==" %%A in ('wmic printer where "default=True" get name /value ' ) do (
setlocal EnableDelayedExpansion
set DefaultPrinter=%%A
echo %DefaultPrinter% |find "\\"
if ERRORLEVEL EQU 1 (goto Local
else (
if ERRORLEVEL EQU 0 goto Shared
)
:Shared
net use lpt3 "%DefaultPrinter%"
goto end
:Local
net use lpt3 "\\%computername%\Imprimant"
goto end
)
:end
exit /B
Is there something missing?
Voila, here you go !! The final script works fine.
#echo off
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
set minimized=
goto :EOF
:minimized
echo Erasing previous mappings from LTP3...
net use lpt3 /d >nul
echo Detecting Default Printer ...
FOR /F "tokens=1-3* skip=2 delims==" %%A in ('wmic printer where "default=True" get name^,servername^,sharename /value ' ) do (
setlocal EnableDelayedExpansion
set DefaultPrinter%%A=%%B
)
echo Default Printer: %DefaultPrinterName%
REM Is this a Local or Network queue?
echo %DefaultPrinterName% |find "\\" >nul
if %errorlevel%==0 ( goto Shared ) else ( goto Local )
:Shared
echo Mapping to LPT3 over %DefaultPrinterServerName%\%DefaultPrinterShareName%
net use lpt3 "%DefaultPrinterServerName%\%DefaultPrinterShareName%"
goto End
:Local
echo Using a Local Printer
IF NOT "%DefaultPrinterShareName%"=="" (
echo Mapping to LPT3 over \\localhost\%DefaultPrinterShareName%
net use lpt3 "\\localhost\%DefaultPrinterShareName%"
) ELSE (
echo Mapping to LPT3 over \\localhost\Impriman
net use lpt3 \\localhost\Impriman
)
:End
exit >> our you can attach other instructions to continue doing some tasks.
So this script will do what I meant to do.
I am not really a programmer. I am a newbie.
Can you guys please help me with a windows batch file that will run a computer test?
Detect any hard drives then run a chkdisk on them
Detect any usb port
Detect any flash drive plug in the pc
Check the display pixels
I have started with this to detect the USB but somehow getting some errors
#echo off
setlocal EnableDelayedExpansion
set PNPDeviceID=4002FDCCE0E4D094
set Q='wmic diskdrive where "interfacetype='USB' and PNPDeviceID like '%%%PNPDeviceID%%%'" assoc /assocclass:Win32_DiskDriveToDiskPartition'
echo %Q%
for /f "tokens=2,3,4,5 delims=,= " %%a in (%Q%) do (
set hd=%%a %%b, %%c %%d
call :_LIST_LETTER !hd!)
goto :_END
:_LIST_LETTER
(echo %1 |find "Disk ") >nul|| goto :_EOF
for /f "tokens=3 delims==" %%a in ('WMIC Path Win32_LogicalDiskToPartition ^|find %1') do set TMP_letter=%%a
set Part_letter=%TMP_letter:~1,2%
echo %Part_letter% %1
goto :_EOF
:_END
:_EOF
:: *** end
pause
Here is a batch script that does almost everything you need.
Checkdisk will run against any local fixed drives. You can have it run in a new window (chkdskAsync=true) or have it run inside the program (chkdskAsync=false). Additionally, specify the chkdsk flags using the chkdskLaunchArguments= variable.
#ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION
:: Use these lines to specify how CHKDSK and Notepad will run
:: ===============================================
SET chkdskLaunchArguments=/F /R
SET chkdskAsync=true
SET launchNotepad=true
:: ===============================================
COPY /Y NUL C:\Users\public\data.txt >nul 2>&1
ECHO Getting drive(s) information
FOR /F "tokens=*" %%A IN ('wmic logicaldisk get caption^, description^, providername^, volumename') DO (
ECHO.%%A>>C:\Users\public\data.txt
)
FOR /F "tokens=1,2 skip=1" %%A IN ('wmic logicaldisk get caption^, drivetype') DO (
IF %%B EQU 3 (
IF "!chkdskAsync!" EQU "true" (
START CMD /C CHKDSK %%A %chkdskLaunchArguments%
) ELSE (
CHKDSK %%A %chkdskLaunchArguments%
)
)
)
ECHO Getting resolution information
FOR /F "tokens=*" %%A IN ('wmic path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution') DO (
ECHO.%%A>>C:\Users\public\data.txt
)
ECHO Information gathered ... saved to C:\Users\public\data.txt
ECHO Showing results.
IF "!launchNotepad!" EQU "true" (
START /WAIT notepad.exe /A C:\Users\public\data.txt
)
ENDLOCAL
EXIT
When it is complete it saves everything to C:\Users\Public\data.txt. It will also launch the file in notepad (if launchNotepad=true is set to ... true).
The output looks like so:
Caption Description ProviderName VolumeName
A: Network Connection \\somefileserver.domain\ New Volume
C: Local Fixed Disk
E: Removable Disk someuser Drive
H: Network Connection \\somefileserver.domain\users$\someuser Data Share and User Home Dirs
P: Network Connection \\somefileserver.domain\Data Data Share and User Home Dirs
S: Network Connection \\somefileserver.domain\share
CurrentHorizontalResolution CurrentVerticalResolution
1920 1080
NOTE: Make sure to run this as an administrator. If you want chkdsk to run
Unfortunately, there isn't a way to grab all USB devices other than storage. Hope this helps!
I didn't realize this question is 4 years old. Going to leave my answer for posterity and anyone digging for answers.
I would recommend trying
chkdsk volume=C:\ D:\ E:\ F:\
to check the disks each as a separate volume
My requirement is I am Creating a text using dos command to store date using
Echo %date% > Sample.txt
Now I should read the Sample.txt to check for the date value and based on the current date I should call another batch file
This thread ponders your question and has at least one solution:
http://www.computerhope.com/forum/index.php?topic=112106.0
create another file with today's date
compare the files with FC
use FIND to check if FC's output says no differences
Use FIND's returned error level to branch your batch file
FC c:\Download\TodayFile.txt c:\Yesterday\File.txt /L | FIND "FC: no dif" > nul
IF ERRORLEVEL 1 goto different
echo Files are the same.
goto end
:different echo Files are different.
:end
WRITE
#echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo %ldt% > test.txt
READ
#echo off
for /f %%a in (test.txt) do (
echo %%a
pause
)