i'm looking for a .bat file that, on opening, ask the user what folder he want to select, then, the batch copy a file (python script) in this folder and execute it
for now i use :
xcopy c:/pythonfiletocopy d:/destinationpath
but i dont find a way to make the user choose the destination folder
any idea ?
thank you
You can request user input using the Set command together with its /P option.
Here's an example which should accept typed or pasted input and should also accept drag and drop for directories not containing spaces:
#Echo Off
Set "_in=" & Set /P "_in=Please provide a directory for the file: "
If Defined _in If Exist "%_in%\" Echo XCopy "C:\pythonfiletocopy" "%_in%"
Pause
I have added an Echo on line 3, if you're happy with the output you can remove that.
I know you said you didn't need one but you could JScript launch a GUI directory browser for input.
Here's an example which may do that:
0</* :
#Echo Off
For /F "Delims=" %%A In ('CScript //E:JScript //NoLogo "%~f0" 2^>Nul'
) Do Echo XCopy "C:\pythonfiletocopy" "%%A"
Pause
Exit /B */0;
var Folder=new ActiveXObject('Shell.Application').BrowseForFolder(0,'',1,'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}');
try{new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch(e){};close();
I have added an Echo on line 4, if you're happy with the output you can remove it and line 5.
Related
I have a game that I play and mod a lot, and a lot of the files in the game have file extensions that are in all caps, which bothers me quite a bit. I'm trying to change them all to be lowercase, but there are numerous folders in the game files, so I'm having to be very repetitive. Right now, I'm working with this:
cd\program files (x86)\Activision\X-Men Legends 2\Actors
start ren *.IGB *.igb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\
start ren *.XMLB *.xmlb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\act0\tutorial\tutorial1
start ren *.XMLB *.xmlb
and so on for each and every folder in the game files. I have a very long .bat file where I just have line after line of this but with a different destination folder. Is there a way to streamline this process so I don't have to manually type out each folder name? Also, is there a line that I could add at the beginning to automatically run as an administrator, so I don't have to make sure to run the .bat file as an administrator each time?
I'm not looking for anything complicated, and I'm very inexperienced with coding other than the small amount of stuff I've been able to search up.
Instead of doing it for each folder, use a for /R loop which loops through all subfolders. I would suggest the following code:
#echo off
:prompt
set /p "extensions=What are the up-case extensions you want to convert to lower-case?: "
if not defined extensions (cls & goto:prompt) else (goto:loop)
:loop
for %%A IN (%extensions%) do (
for /R "custom_folder" %%B IN (*.%%A) do (
ren "%%~fB" "%%~nB.%%A"
)
)
Take a look on this on how to run this batch file as admin. Create another batch file and add the code specified in the accepted answer.
Note: As Stephan pointed out in the comments, you can use %ProgramFiles(x86)% environment variable which is the same thing.
#echo off
setlocal
rem Check if admin.
2>nul >nul net session || goto :runasadmin
rem Start in script directory.
pushd "%~dp0" || (
>&2 echo Failed to change directory to "%~dp0".
pause
exit /b 1
)
rem Ask for directory to change to, else use the script directory if undefined.
set "dirpath=%~dp0"
set /p "dirpath=Dir path: "
rem Expand any environmental variables used in input.
call set "dirpath=%dirpath%"
rem Start in the input directory.
pushd "%dirpath%" || (
>&2 echo Failed to change directory to "%dirpath%".
pause
exit /b 1
)
rem Ask for file extensions.
echo File extensions to convert to lowercase, input lowercase.
echo i.e. doc txt
set "fileext="
set /p "fileext=File extension(s): "
if not defined fileext (
>&2 echo Failed to input file extension.
pause
exit /b 1
)
rem Display current settings.
echo dirpath: %dirpath%
echo fileext: %fileext%
pause
rem Do recursive renaming.
for %%A in (%fileext%) do for /r %%B in (*.%%A) do ren "%%~B" "%%~nB.%%A"
rem Restore to previous working directory.
popd
echo Task done.
pause
exit /b 0
:runasadmin
rem Make temporary random directory.
set "tmpdir=%temp%\%random%"
mkdir "%tmpdir%" || (
>&2 echo Failed to create temporary directory.
exit /b 1
)
rem Make VBS file to run cmd.exe as admin.
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "cmd.exe", "/c ""%~f0""", "", "runas", 1
) > "%tmpdir%\getadmin.vbs"
"%tmpdir%\getadmin.vbs"
rem Remove temporary random directory.
rd /s /q "%tmpdir%"
exit /b
This script is expected to start from double-click.
It will restart the script as admin if not already admin.
It will prompt to get information such as directory to change to and get file extensions i.e. doc txt (not *.doc *.txt). If you enter i.e. %cd% as the directory input, it will be expanded.
This is My first post in here
I need a help for your side I have made a batch file for run autocad exe and load a script file but give error when I run the batch file
#echo off
set KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools
set VALUE_NAME=installpath
set FN=loadload
set FE=scr
FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B)
set approot=%pInstallDir:~0,-1%
echo %approot%\%FN%.%FE%
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %approot%\%FN%.%FE%
pause
Error: while running batch file autocad opens and in commandline the script file not loading "Xpresslisp.scr": Can't find file."
and bellow one is working
script file loading without getting error
#echo off
set path=%USERPROFILE%
set fol=Documents
set NAME=1
set SUFFIX=scr
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %path%\%fol%\%NAME%.%SUFFIX%
pause
Regarding your second question in the comments...
Bellow command will create the text file and write the first line to it e.g. "some text" like in the command below.
Echo some text > full_path_to_txt_file
Command below will append new text to same file.
Echo some text >> full_path_to_txt_file
'>' char creates file and writes firs line
'>>' char append text
check that %path%\%fol%\%NAME%.%SUFFIX% returns the Full Path to the "Xpresslisp.scr" file !
if it does, inspect the Full Path and see if it contains any white spaces.
if it does, enclose the %path%\%fol%\%NAME%.%SUFFIX% with apostrophes
"%path%\%fol%\%NAME%.%SUFFIX%"
It may be something as simple as blindly removing the last character of the installpath without knowing for sure what it is, (doublequote or backslash?).
As there is unlikely to be multiple copies of any filename in the Xpresslisp Tools tree, I would suggest something like this:
#Echo Off
Set "KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools"
Set "VALUE_NAME=installpath"
Set "FN=loadload"
Set "FE=scr"
(Echo=FILEDIA 0
Echo=(LOAD "C:\\loadmyfile.lsp"^)
Echo=FILEDIA 1)>%FN%.%FE%
For /F "Tokens=2*" %%A In ('Reg Query "%KEY_NAME%" /v "%VALUE_NAME%"') Do (
For /F "Delims=" %%C In ('Dir/B/S/A-D "%%~B"\"%FN%.%FE%" 2^>Nul') Do (
Start "" "%ProgramFiles(x86)%\AutoCAD 2002\acad.exe" /b "%%~C"))
This doesn't care if there is a trailing backslash or not and will only run the AutoCAD command if the file is there.
I have searched online for this, but can't seem to find an answer. I have code that creates a text file after asking one what name he/she would like to give the text file. The file is then opened after being created. However, if I choose N (No) to an overwrite request, I don't want the file to open. I would instead want to be asked again to specify another file name after saying N (No) to the overwrite request.
I however have no idea as to how to check the answer given to the overwrite request.
Also, I would need the entire code to be in one line.
This is what I have without the extras that I am mentioned above:
cmd /c #ECHO OFF & SET /P filename=What File name: & cmd /v /c copy /-y NUL !filename!.txt & cmd start /v /c start notepad !filename!.txt & Exit
Later Added:
I am working on the following and keep getting the error message "( was unexpected at this time." after the filename is echoed. If I edit the file by commenting parts out so that it works, and then continue to edit it to what I currently have, it still works. However, if I exit the cmd box, and then start it again, then it doesn't work and I get an error message. This is the code thus far:
#echo off
SET /P filename=What File name?
echo %filename%
::loop
if exist !filename!.txt (
echo File Exists
SET /P overwrite=Overwrite File?
echo overwrite is %overwrite%
If %overwrite%==Y (
echo "Yes, Y"
)
)
echo finish
If I put single quotes around %overwrite% in If '%overwrite%'==Y I no longer get the unexpected error message. The problem I still face though is when exiting the session / the cmd box, I can't get the line above to echo the value of overwrite. It just says "overwrite is" (without quotes and with no value after it). If I continue in the same session running the batch file over again, I get a value for overwrite.
Solution to unable to echo value of %overwrite% (user input) from if statement can be found here.
edited to adapt to comments
cmd /v:on /q /c "for /l %a in (0 0 1) do (set /p "file=file? " & if exist "!file!" ( set "q=" & set /p "q=overwrite? " & if /i "!q!"=="y" ( type nul > "!file!" & start "" notepad "!file!" & exit ) ) else ( type nul > "!file!" & start "" notepad "!file!" & exit ))"
As requested, in one line. To include it inside a batch file, replace %a with %%a
edited one line, with input validations and error checks added on file operations, and shortened (variables length reduced, unneeded spaces removed, file creation code deduplicated, ...) to fit into windows "Run" dialog
edited added initial cd /d "%userprofile%" to ensure the working folder is writeable. Why? Because from windows 7, including the /v:on (or off) in the call to cmd from the windows Run dialog (the OP reason for a one liner), the active directory for the command is c:\windows\system32 (or wherever the system is). Without the /v switch, the active directory is the user profile folder.
cmd /v:on /q /c "cd/d "%userprofile%"&for /l %a in () do ((set/p"f=file? "||set "f=")&(if defined f if exist "!f!" ((set/p"q=overwrite? "||set "q=0")&if /i not "!q!"=="y" (set "f=")))&if defined f (type nul>"!f!" &&(start "" notepad "!f!" &exit)))"
edited After a lot of tests i make it fail in XP. The previous code will fail if the machine is configured with command extensions disabled. Also, the problem with Ctrl-C can be anoying. This should handle the first problem and minimize the second.
cmd /v:on /e:on /q /c "cd/d "%userprofile%"&for /l %a in ()do ((set/p"f=file? "||(set f=&cd.))&(if defined f if exist "!f!" ((set/p"q=overwrite? "||(set q=&cd.))&if /i not "!q!"=="y" (set f=)))&if defined f (cd.>"!f!"&&(start "" notepad "!f!" &exit)))"
It is better to avoid overwriting by copy in this case (because it's ERRORLEVEL ignores overwriting status). You can do everything on your side:
You can check existance of file (IF EXISTS !filename!.txt),
If file exists, you can ask user what to do (SET /P userInput=Overwrite? (Yes/No/All)),
After it you can analyzed %userInput% to decide what to do (delete existent file and create empty one with the same name + open editor or ask file name again).
If %overwrite%==Y : if %overwrite% is empty, this is executed as If ==Y - obviously a syntax error.
With the singlequoutes you mentioned: If '%overwrite%'==Y is executed as If ''==Y - this is proper syntax, so your code doesn' fail (but is not running as intended)
The reason why %overwrite% is empty: you are using it inside a block (between if ( and the corresponding ), so for parsing reason it' easy (search for delayed expansion).
You can easily avoid that:
#echo off
SET /P filename=What File name?
echo %filename%
:loop
if not exist %filename%.txt goto finish
echo File Exists
SET /P overwrite=Overwrite File?
echo overwrite is %overwrite%
If "%overwrite%"=="Y" ( echo "Yes, Y" ) else ( goto loop )
this line is never reached
:finish
echo finish
When using a single line you can't loop back to the start - but this will only create the file AND start notepad with the file, if the file does not exist.
If the file exists it will just exit and NOT start notepad so you know that you have to try again.
cmd /c #ECHO OFF & SET /P filename=What File name: & cmd /v /c "if not exist !filename!.txt copy NUL !filename!.txt & start "" notepad !filename!.txt" & Exit
I am trying to make a batch file to delete malicious files from pendrive. I know that these malicious files uses hidden,read only and system attributes mainly to hide itself from users. Currently i am deleting these files using cmd by removing malicious files attributes then deleting it. Now I am thinking to make a small batch file which can be used to remove these files just by entering the drive letter.
I have found this code in a website to find attributes of a file. But after entering the name of the file the batch file just exits without showing any results.
#echo off
setlocal enabledelayedexpansion
color 0a
title Find Attributes in Files
:start
set /p atname=Name of the file:
if not exist %atname% (
cls
echo No file of that name exists!
echo.
echo Press any key to go back
pause>nul
goto start
)
for /f %%i in (%atname%) do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
echo Press any key to go back
pause>nul
goto start
can you tell me why this batch file is exiting without showing any results. Or can you give any better batch script for getting attributes of a file.
EDIT
I was able to work the above code only for a single file. As my purpose of my batch file is to remove malicious files by entering the drive letter. How can i use it to find what kind of attributes files are using in a particular drive.
For example:
In cmd we can use this command to find the file attributes of a given drive
attrib *.*
Advance thanks for your help
I tried the bat file (without inspecting the details) and it seems to work fine for me. What I noticed is that it closes instantly if you don't enclose file path with quotation marks - e.g. "file". Example:
Name of the file: path\file.txt // this will close immediately
Name of the file: "path\file.txt" // now it will stay open and display the result
This hopefully solves your problem.
As far as your question in EDIT is concerned, a simple option is to iterate a list of files and execute the batch on each one.
batch1.bat: (%1 refers to the first command-line parameter)
#echo off
setlocal enabledelayedexpansion
echo %1
set atname=%1
for %%i in ("%atname%") do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
Next, generate a list of all files within a given path (say 'folder' including all subfolders):
dir /s /b folder > ListOfFiles.txt
main.bat (read ListOfFiles.txt line-by-line and pass each line to batch1.bat as a command line parameter):
#echo off
for /f "tokens=*" %%l in (ListOfFiles.txt) do (batch1.bat %%l)
Then, from cmd:
main.bat >> output.txt
The last step generates an output file with complete results. Granted, this can be done in a more polished (and probably shorter) way, but that's one obvious direction you could take.
You're using a for /f loop here, which isn't necessary (and may yield undesired results if the filename contains spaces). Change this:
for /f %%i in (%atname%) do set attribs=%%~ai
into this:
for %%i in ("%atname%") do set attribs=%%~ai
This is dangerous code - but it'll delete read only, hidden and system files.
It should fail to run on c: drive but I haven't tested it. Note that some Windows installs are on drives other than c:
#echo off
echo "%cd%"|find /i "c:\" >nul || (
del *.??? /ar /s /f
del *.??? /ah /s
del *.??? /as /s
)
So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause