I am working with a batch script in which the user can automate the creation of folders and subfolders on windows.
Here's the actual code:
#echo off
:: MAKES A TEMPORARY FOLDER FIRST
mkdir "New Folder 123"
:: LET THE USER INPUT PREFERED FOLDER NAME AND THIS WILL CHANGE THE TEMPORARY INTO THE NEW ONE
:rename
cls
SET /p comm=Please enter your prefered folder name:
IF /i "%comm%" == "%comm%" ren "New Folder 123" "%comm%"
IF /i "" == "%comm%" goto :rename
IF /i " " == "%comm%" goto :rename
:: COMMAND WILL OPEN THE NEWLY RENAMED FOLDER AND CREATES SUBFOLDER(S)
cd "%comm%"
md "Folder 1" "Folder 2"
exit
Now, the problem is... If the user accidentally exits the command prompt window, the temporary folder which I initially wrote from the beginning of the code remains.
:: MAKES A TEMPORARY FOLDER FIRST
mkdir "New Folder 123"
This will create a conflict if the user runs the batch script the second time since that temporary folder already exists and I don't want the user to manually right click and delete it cause that would be a hassle.
So I'm hoping if anyone could provide a code that will automatically delete the initial folder as the user (accidentally) exits the console? I've been looking for similar solutions here that are closely related to mine, but they all seems to delete the batch file itself instead of a folder, I'm also really new into making programs and I had a hard time understanding some batch codes, so please be easy with me, Any help would be greatly appreciated, Thank you so much everyone.
Just add this at the beggining of the file after #echo off
rmdir "New folder 123" /s /q >NUL 2>&1
Related
I am trying to make a little shortcut for my daily work. I often have to copy some files from let's say C:\folder0\folder1\aaaa\ to C:\folder0\folder1\bbbb\.
I want to create a batch file shortcut in the send-to menu. So I would first select the files and then click on the new added shortcut to the batch file which should do the rest.
#echo off
:here
if '%1'=='' goto exit
"C:\Program Files (x86)\Notepad++\notepad++.exe" "%1"
echo %cd%
shift
goto here
:exit
I started with opening the files in Notepad++ and displaying the path.
But I need a function that stores the path from the given files and changes the folder a to folder b. Afterwards it would take the new path for the standard copy function.
xcopy /s C:\source D:\target
I hope I could properly explain what I try to achieve.
I found a solution. This is my code and it is working for me now as long as there are no spaces in the path (has someone an idea to fix that?)
#echo off
:here
if '%1'=='' goto exit
set strpath=%cd%
set strresult=%strpath:folder1=folder2%
#echo The original file '%1'
#echo New path %strresult%
coyp /b/v/y "%1" "%strresult%"
shift
goto here
:exit
pause
im having a little issue, I currently made a simple batch script to open this program and a specific file so that users dont have to go into the directory and open it that way, they only enter the file folder name and file name and it opens that specific file straight away, however I need it so that the user enters just the file name, I will show the simple working version which you have to type in the folder name aswell. I will also show the version I am trying to get to work without having to type the folder name, however the if statement is not working as it always runs both parts of it.
#echo off
set /p id2=Enter folder name for the label.
echo %id2%
set /p id=Enter Label Code, please use this format E.G. AA-01:
echo %id%
Start "" "C:\Program Files (x86)\EuroPlus\NiceLabel 6\bin\NicePrint.exe" "J:\MYFOLDER\%id2%\%id%.lbl"
^ This is the working version however it is tedious as you have to type the folder name.
#echo off
set STARTDIR="J:\MYFOLDER"
set /p id=Enter Label Code, please use this format E.G. AA-01:
echo %id%
for /f "delims=" %%a in ('dir /s /b "J:\MYFOLDER"') do echo %%a
if exist %id% in ('dir /s /b "J:\MYFOLDER"') (
echo File found, starting NicePrint.
/! set current folder where file was found
The file path is %~dp1
Start "" "C:\Program Files (x86)\EuroPlus\NiceLabel 6\bin\NicePrint.exe" "J:\MYFOLDER\%~dp1\%id%"
) else exit
pause
Problems with the second part: If statement always prints file found starting nice print even if user input doesnt match a file and launches the program, further more, I need the directory of that file from this so that I can then set the program to launch that file after it starts as you can see I have attempted but I am still a novice to things like this as I am not too good at programming/scripting etc.
Thank you for help in advance.
So I have been searching in other topics how to rename files and folders in a .bat in a recursive way but it isn't working at all.
My code is:
# echo off
setlocal enabledelayedexpansion
set /p rut="Introduce folder: "
FOR /D /R %%x in ("%rut%"\*) DO (
cd %rut%
echo %cd%
pause
ren .\* "a"
)
exit
But this only renames the files that are on the first folder and not in the rest. Forfile won't work at all with the variables.
What I want to get is how I could rename everything inside the main folder (including subfolders) whatever it is, be it files or folders as "a" to solve the Windows problem of having routes way too long when trying to delete a full structure, that is why I can't use the ".txt" ".whatever" solution.
A powershell solution would be valid too!
Thank you very much
Edit I added a random to see if it was the name that was conflicting but no, it is still not working:
ren .\* "a%RANDOM%"
And renaming them from cmd works the same way, I mean, if I write ren "folder" "whatever" it will change but in the script doesn't work with "*"
I am trying to create a .BAT file in DOS 6.22 that will copy the contents of a floppy disk in A: over to C:\ and then set the folder created as a system variable. I tried using something like "SET /P VARIABLE=Enter a path" however DOS will just add "/P VARIABLE" as a variable with the value of "Enter a path" so using the /P isn't an option as /P wasn't a switch in DOS 6.22
I tried using something like a for loop to set a variable to the file however where I hit a speed bump is that I have no idea what the folder is going to be called in drive A:\ as it will change all the time but only ever contain one folder, so basically I am just trying to find a way a way to copy the first directory found in drive A over to C:\ and set that as a system variable. As once the user is done making changes I will have to copy that folder back over to A:\ and overwrite the old files so it can be stored on the network once changes have been made.
I did try experimenting with some If/for statements through a .BAT file but I didn't have much luck with theses, if anyone could point me in the right direction that would be awesome.
At this point I'm probably making this way more complicated than I have to.
something like this should work too:
#echo off
:INPUT.BAT puts what is typed next in environment variable INPUT
set input=
echo INPUT.BAT
echo Type in something and press [Enter]
fc con nul /lb1 /n|date|find " 1: ">temptemp.bat
echo :Loop>>enter.bat
echo if not (%%input%%)==() set input=%%input%% %%5>>enter.bat
echo if (%%input%%)==() set input=%%5>>enter.bat
echo shift>>enter.bat
echo if not (%%5)==() goto Loop>>enter.bat
for %%x in (call del) do %%x temptemp.bat
del enter.bat
echo The string you just entered:
echo %input%
echo has been stored in an environment variable named INPUT
:End
I ended finding a solution after doing some more research from what #Squashman linked. Turns out there was a breakdown in communication and this wasn't even the original issue that the user had (a simple way to copy the files off A:\ and all that)
I used the following.
echo Type "set myvar="name of the folder" replacing name of the folder with
echo the name of the folder containing the files on A:\ example if you were
echo on "example" you would type: set myvar=example
copy con answer.bat
echo Type the words "set myvar=" (don't type the quote marks)
echo and then immediately after the = sign, press Control-Z then enter
call answer.bat
mkdir C:\%myvar%
xcopy A:\%mvar% C:\%myvar%
DEL answer.bat
This is a modified version of a guide I found here. http://www.pement.org/sed/bat_env.htm#4dos
Hopefully this is able to help someone, this isn't pretty by any stretch of the imagination but it worked.
I think this would be easier for your users if they actually need to only copy one directory.
#echo off
if "%1" == "" goto syntax
md C:\%1
xcopy/E A:\%1 C:\%1
goto end
:syntax
echo Please input a directory after %0
:end
I need to create a batch file to do a few things for a class I'm in. Everything needed to run works but I cannot find a simple answer as to why change directory (CD) is not working for me. I can't figure it out.
My code is as follows:
#echo off
title NewUser
: creating a folder called "Scripts" on C:\
: add local user named: "MaxLocal" password: "student"
: create directory at the root of F:\ named "Files_for_Max"
: create ACE for user "MaxLocal" to "Files_for_Max" with: Read, read & Execute, List folder contents
: Re-establish inheritence to sub folders and files
: copies cmd.exe from C:\Windows\System32 folder to "Files_for_Max"
: add "MaxLocal" to management group created in Assignment 3
: produces ICACLS report for "Files_for_Max" called "icaclsReport.txt" in "Scripts"
: moves this .bat file to "Scripts"
mkdir "C:\Scripts"
net user MaxLocal student /add
mkdir "F:\Files_for_Max"
icacls "F:\Files_for_Max" /grant MaxLocal:(OI)(CI)RX
copy "C:\Windows\System32\cmd.exe" "F:\Files_for_Max"
net localgroup Management MaxLocal /add
icacls F:\Files_for_Max /save C:\Scripts\icaclsReport.txt /t
move "F:\NewUser.bat" "C:\Scripts"
pause
So it's the last line specifically. MOVE works great but I'm not allowed to use it for whatever reason. I have tried a ton of ways of how to do it with CD to no conclusion. I need to take this NewUser.bat file on F and move it to the newly created scripts folder. Yes I have tried the /D command but perhaps I didn't have the correct spacing or used quotes when unneeded?
Any ideas?
cd /d "C:\Scripts"
This changes the current working directory to C:\Scripts.
If you want to instead move the Batch file itself:
copy "%~f0" "C:\Scripts"
(goto) 2>nul & del "%~f0"
This copies itself (%~f0) into C:\Scripts, then deletes itself, effectively a move command.
If you want you could do this:
copy "%~f0" "C:\Scripts"
start "C:\Scripts\~nx0"
(goto) 2>nul & del "%~f0"
Which also starts the copied Batch file, then it deletes itself.
You could have used cd before the copy command, which may correspond to how you need to use it before the move command.
cd C:\Windows\system32
copy cmd.exe F:\Files_for_Max
The batch file asks "copy what where" so we have copy for copy, cmd.exe for what, and F:\Files_for_Max for where.
Similarly,
cd F:\
move NewUser.bat C:\Scripts
Should be the way it can work. So first you're moving into the directory F:\ before asking it to move the specified file, much like what I have in the copy command.