Im not used to working in batch. I've made a database with back end and a light frontend(1mb). This front end gets updated many times so i was trying to make a batch for the users that functions as a shortcut. It should make a copy of the newest frontend save it to a subfolder and name it with the username, then open that copy.
If the user copy already exists it should just open it again.
So far i got a batch like this but it doesnt work:
if exist "H:\Database\AccountDb\%USERNAME%.accde" (
start "H:\Database\AccountDb\%USERNAME%.accde" /f /s /q
) else (
xcopy "H:\Database\frontend.accde" H:\Database\AccountDb\%USERNAME%.accde" /t /e /i /y
start "H:\Database\AccountDb\%USERNAME%.accde" /f /s /q
)
The first argument to START is treated as the window title if it is quoted. You simply need to add an empty title argument when the command must be quoted.
Also, having quotes in the middle of the path is OK, but it doesn't look particularly good. Most people put quotes around the entire path.
start "" "H:\Database\AccountDb\%USERNAME%.accde" /f /s /q
Related
I am trying to delete the sub folders using batch file.
I have folder
c:\users\mis
c:\users\mis\A\Third Party Log
c:\users\mis\B\Third Party Log
c:\users\mis\C\Third Party
c:\users\mis\D\Third Party
Etc.
I want to delete all "Third Party Log" folder and I tire as below:
#echo off
Set MainFolder=C:\users\mis
FOR /D %%D IN ("%MainFolder%\*") DO CALL :RENAME %%D
pause & exit
REM -------------------------------------------------------
:RENAME
Set CRITERIA=Third Party Log
FOR /D %%R IN ("%1\%CRITERIA%") do #if exist "%%R" rd /s /q "%%R"
REM -------------------------------------------------------**
If the folder is only Third it is deleting. Please help me deleting the fodlers.
Thanks,
Htet
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "targetdir=u:\your files"
SET "selectname=Third Party Log"
FOR /f "delims=" %%a IN (
'dir /b /s /ad "%targetdir%\%selectname%" '
) DO (
IF /i "%%~nxa"=="%selectname%" ECHO RD /S /Q "%%a"
)
Simply perform a dir /b /ad to list all of the directorynames matching the mask %selectname% and remove all of the hits.
Note that the for loop could be condensed to a single line, but I've split it over a number for clarity.
Naturally, the variables could be substituted by their literal values as well, if desired. This would reduce the entire batch to a single line which could be entered directly at the prompt in which case each %% needs to be reduced to a single %.
Posted code simply echoes the proposed instruction to allow for verification. I the displayed instructions are correct, remove the echo keyword to actually destroy the unwanted directories.
Finally, in your original code, rename is not a good name for the subroutine for two reasons : First, it is misleading as no renaming is being performed and second, ren is an alias of rename - and it's not a good idea to have a routine or variable or label that is itself a command-name.
I can sadly not write code and test it rn but:
You could use the dir command into a temp file.
Use findstr(or find i m not shure) and look for third party log.
Write this back to the temp file.
Now you should be left with a file filled with the paths of the folders you want gone.
Read the file line by line and delete and use this line string to provide the path for the del command.
To change the name as in the sample code you could take the line string and use it as the path to the ren command.
This is most likely a very bad approach but it should work.
Maybe I'm trying to oversimplify things. Having trouble removing part of a variable to be used as a path name. Goal is to transfer everything from folders (and sub) on multiple windows profiles to a single profile.
I can copy all the content, but cannot tell it where to place it. Also happen to be trying to use robocopy instead for the first time. I want to make a folders on the desktop that are named the username of every user on the device, then copy all content.
for /d %%a in (C:\Users\*) do (
set var=%%a
var=${var#*s/ }
mkdir "%USERPROFILE%\Desktop\Backup\%var%\folderx"
xcopy "%%a\Desktop\folderx" "%USERPROFILE%\Desktop\Backup\%var%\folderx" /E /Y
)
Notes:
Not sure the best way to remove C:\Users\ so I leave just the username as a variable (it assumes var is a command)
I'm trying to do it without having to write a string to file and read it back
This assumes the Backup folder is already created
Still not sure how to strip it correctly. Just found it more efficient to not include the drive in the first place.
List item dir bare command to get name
changed C: to user variable in case installed on a different letter
FOR /f "tokens=*" %%a in ('dir /b /a:d-s-l "%SystemDrive%\Users"') DO (
mkdir "%USERPROFILE%\Desktop\Backup\%%a\folderx"
mkdir "%USERPROFILE%\Desktop\Backup\%%\foldery"
xcopy "%SystemDrive%\Users\%%a\Desktop\folderx" "%USERPROFILE%\Desktop\Backup\%%a\folderx" /e /y
xcopy "%SystemDrive%\Users\%%a\Desktop\foldery" "%USERPROFILE%\Desktop\Backup\%%a\foldery" /e /y
)
Strictly speaking, not an answer, but a cleaner solution to the root problem. Still not sure how to properly strip information.
Can someone advise where I have gotten wrong? I'm looking to run all files in a folder where the filename contains the word Expenses
cd /D Z:\System Administrator\System Backups\Scripts\
FINDSTR /S /M "expenses" *.*
FOR /F %%i in ('findstr /I /S /M "expenses" *.py') DO command %%i
findstr will get me the list of files I need, I just can't get them to run and get the error that command is not recognised as an internal or external command.
The list of files will be
Expenses 2017-18.py
Expenses 2018-19.py
etc
Thanks in advance
UPDATE
I have found that this works, although I welcome any improvements
for /R %%f in (*expenses*.py) do start python "%%f"
I believe that what is being sought is the instruction to actually execute the python files %%i. I'm no python expert - I don't know whether you need to simply use whatever.py from the prompt or somexecutablename whatever.py.
I believe that you'd probably need call "%%i" to run the files serially or, to run in parallel, use start "" "%%i". In either case, if you need somexecutablename then insert that before "%%i".
The empty string in the start version is the windowtitle. It's really needs to be there, even if empty - but you could replace it with "%%i" to show the executing filename as the window title.
Newbie question.
Would cd "C:del *.* delete all files within the C: drive?
I was also told if I add del . /F /Q it would bypass confirmation of the deletion, but I'm not 100% sure where to put it. Do I just add it onto the end like
cd "C:del *.* del . /F /Q?
This is a dangerous command. Be careful.
No, you probably want del /F /Q ..
The first part, del, is the command. This is telling it to delete something.
/F /Q are the options, denoted by the leading slash.
/F means to force it, so for example if a file isn't writable it will still delete it if able. It also skips some other checks.
/Q tells the program to be 'quiet', so less output will be generated than normal
.(dot) is the thing to delete, which is the place you currently are. If you are currently in C:\ (ie if the terminal displays C:\> at the beginning of every line), then it will be that that it deletes.
Note that I have only shown what you should type (del /F /Q .) and not what the entire line should look like when you're done (C:\> del /F /Q .). However, if this is going in a batch file then you want the first form only.
This is a destructive command and is provided merely to answer the query - don't try it at home or prank friends with it.
This will delete all files that are not locked, or opened for exclusive access by a program.
del c:\* /s /f /q /a
It will not delete every file in a Windows system drive and will not remove directories.
I have a follow bat command which will delete unnecessary files when i give respective file extension
#ECHO OFF
REM Change the path and the extension...
DEL /s /f /q "C:\Users\dell\Desktop\test\*.pdf"
DEL /s /f /q "C:\Users\dell\Desktop\test\*.csv"
ECHO Are you sure ? Press a key to continue.
PAUSE > NUL
ECHO Done
PAUSE
I am in need to insert specific sub-folder to delete along with files. Assume specific sub-folder name may be abc
Can any body help me on this how to insert "delete specific sub-folder"
Use the RD command to remove a (sub)directory. Since a directory may not be empty, you may want to add the /S switch to remove the entire directory tree. And since you are also going to use the command in a batch script, you may need the /Q switch as well, to avoid the confirmation prompt.
So add a command like this to your script:
RD /S /Q "C:\Users\dell\Desktop\test\Specific\Subfolder"