Change Directory command in batch file is not working - batch-file

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.

Related

Delete the folder the executing batch-script lies in

I have a win-cmd-script "a" saved in folder "f", and want to delete the folder "f" with the script itself. Is that possible, and if yes, how?
I know, that a batch-file can delete itself:
(goto) 2>nul & del "%~f0"
And now I have tried:
set mypath=%~dp0
rmdir %mypath%
But that doesn't work. Any ideas?
You can't remove a folder if any process has the folder as the active folder. If you want to remove it, you need to ensure the cmd process executing the script has moved to a different folder:
#echo off
setlocal
set path_to_del=%~dp0
cd /d %USERPROFILE%
rmdir /s/q "%path_to_del%"
This will remove the folder containing the script, though, it will error out as it attempts to scan the file for the line after the rmdir call. Also, if you have any other tools open in this folder, such as notepad, then the directory will remain.

How to delete files matching a certain pattern in their name?

I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.

Script that deletes itself by mistake

This batch script is supposed to map to PC and rename the files in that folder using the date & time stamp and copy them to another location on a different PC. It should then delete all the files in that source folder except for a file that called "LBBS.log". It all works fine except for the delete part. It isn't deleting anything in the folder and it is actually deleting the batch file itself. When I run it, it copies over fine but then it deletes itself. Can someone please tell me what I need to change for this to work. What am I missing? Its on a windows 7 environment. Thanks in advance.
net use x: \\MTLLBBS023\C$
set "stamp=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%"
set "source=MTLLBBS023"
xcopy /S /E /I x:\logs E:\Data\Logs\MTLLBBS023\%source%-%stamp%.*
cd x:\Logs
for %%i in (*) do if not %%i == LBBS.log del %%i
net use x: /delete
The problem is that your script and target directories are located on separate drives.
When you cd to another directory, the command will fail if you try to move to another drive without using the /d option.
Instead of cd x:\logs, you should say cd /d x:\logs - this will change the drive and directory.
Alternatively, instead of the net use and net use delete commands, you can simply pushd \\MTLLBBS023\C$ to go to the network drive (this also automatically creates a temporary network drive) and then popd at the end of the script to leave the directory and remove the mapped drive. This way, you don't need to cd at all.

Copying a Folder and renaming it using command prompt

I am trying to copy a folder and paste it in the same directory it was copied from.
For example
C:\Test is the main directory which consists of a folder ACDM, I would like to copy ACDM in the same directory and rename the new folder to ACDM1 which will have all the same files as ACDM has
I would like to do it using command prompt
I tried the following
C:>Xcopy C:\Test C:\Test\ACDM1 /E /U
Cannot perform a cyclic copy
0 File(s) copied
which fails, not sure hoe to add REN command with XCOPY command.
Need help ASAP as i would want to create a batch file which will create a copy of an existing folder and rename it according to a name retrieved from a text file..
xcopy "C:\Test\ACDM\*.*" "C:\Test\ACDM1\" /s/h/e/k/f/c
for /f "delims=" %%a in (yourtextfilename) do xcopy "C:\Test\ACDM" "C:\Test\%%a\" /E
as a .bat file line. Directly from the prompt, change each %% to %
I've assumed (for lack of futher information) that your textfile contains just the one line
ACDM1
neither do you specify the textfilename tou want to use.
xcopy C:\Test\ACDM C:\Test\ACDM1\ /E /Q

Batch script to delete files

I have a batch script as follows.
D:
del "D:\TEST\TEST1\Archive\*.TSV"
del "D:\TEST\TEST1\Archive\*.TXT"
del "D:\TEST\TEST2\Archive\*.TSV"
del "D:\TEST\TEST2\Archive\*.TXT"
del "D:\TEST\TEST 100%\Archive\*.TSV"
del "D:\TEST\TEST 100%\Archive\*.TXT"
The above code deletes all the ".txt" and ".tsv" files from all the folders except from the folder TEST 100%. For deleting the files from TEST 100% i am getting the error as The Path could not be found. I guess the % symbol in the folder name creates the issue.
Can anyone guide me to resolve the issue and to delete the files from the folder TEST 100%?
You need to escape the % with another...
del "D:\TEST\TEST 100%%\Archive*.TXT"
There's multiple ways of doing things in batch, so if escaping with a double percent %% isn't working for you, then you could try something like this:
set olddir=%CD%
cd /d "path of folder"
del "file name/ or *.txt etc..."
cd /d "%olddir%"
How this works:
set olddir=%CD% sets the variable "olddir" or any other variable name you like to the directory
your batch file was launched from.
cd /d "path of folder" changes the current directory the batch will be looking at. keep the
quotations and change path of folder to which ever path you aiming for.
del "file name/ or *.txt etc..." will delete the file in the current directory your batch is looking at, just don't add a directory path before the file name and just have the full file name or, to delete multiple files with the same extension with *.txt or whatever extension you need.
cd /d "%olddir%" takes the variable saved with your old path and goes back to the directory you started the batch with, its not important if you don't want the batch going back to its previous directory path, and like stated before the variable name can be changed to whatever you wish by changing the set olddir=%CD% line.
Lets say you saved your software onto your desktop.
if you want to remove an entire folder like an uninstaller program you could use this.
cd C:\Users\User\Detsktop\
rd /s /q SOFTWARE
this will delete the entire folder called software and all of its files and subfolders
Make Sure You Delete The Correct Folder Cause This Does Not Have A Yes / No Option
Consider that the files you need to delete have an extension txt and is located in the location D:\My Folder, then you could use the below code inside the bat file.
cd "D:\My Folder"
DEL *.txt
in batch code your path should not contain any Space so pls change your folder name from "TEST 100%" to "TEST_100%" and your new code will be
del "D:\TEST\TEST_100%\Archive*.TXT"
hope this will resolve your problem

Resources