I want to open every file that ends with .jar in my Downloads folder. My code doesn't work. What am I doing wrong and how do I achieve my end goal?
Code:
del /s /q "C:\Users\%USERNAME%\AppData\Roaming\.minecraft\session.*"
cls
:choice
cls
set /P c=Open the file? [Y/N]
if /I "%c%" EQU "Y" goto :start
if /I "%c%" EQU "N" goto :choice
goto :choice
:start
start "" "C:\Users\Home Computer\Downloads\????.jar"
del /s /q "C:\Windows\Prefetch\JAVA*.pf"
del /s /q "C:\Users\Home Computer\Recent\*.bat.lnk"
del /s /q "C:\Users\Home Computer\Recent\????.jar.*"
del /s /q "C:\Users\Home Computer\Recent\Downloads.lnk"
pause
Edit: I should also mention that the name of the .jar changes every time I re download it. I can't target it specifically by file name. The file name is always four characters.
Take a look at these two screenshots.
https://gyazo.com/579ebd940bcd74f3a3deeb05db7b337d
https://gyazo.com/ceaffd97bf1f1e81860036810b35fcd7
If the name of the file has same number of characters everytime, then you can use ? for each character instead of *.
Say the filename is 3 characters long, then you can write the command as:
start "" "C:\Users\Home Computer\Downloads\???.jar"
Maybe you can take some references from the link given below:
https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/
try
start "" "C:\Users\Home Computer\Downloads\*.jar"
or
java -jar "C:\Users\Home Computer\Downloads\*.jar"
You could use the Where command to identify the name of the downloaded file. (Of course it will only identify it if you don't have more than one four character .jar file there).
#Echo Off
Del/S/Q "%AppData%\.minecraft\session.*" 2>Nul
:Pick
ClS
Choice /M "Open the file"
If ErrorLevel 3 GoTo :EOF
If ErrorLevel 2 GoTo Pick
If ErrorLevel 1 GoTo Start
GoTo :EOF
:Start
For /F "Delims=" %%A In ('Where "%UserProfile%\Downloads":"????.jar"'
) Do (Set "FullName=%%~fA" & Set "Name=%%~nxA")
Start "" "%FullName%"
Del/Q "%SystemRoot%\Prefetch\JAVA*.pf" 2>Nul
Del/Q "%UserProfile%\Recent\%~nx0.lnk" 2>Nul
Del/Q "%UserProfile%\Recent\%Name%.lnk" 2>Nul
Del "%UserProfile%\Recent\Downloads.lnk" 2>Nul
Timeout -1
You could even shorten it a little by deleting more than one of the shortcuts from the same delete line.The locations you've used for recent are effectively only links to the real location in modern Windows versions so you could replace %UserProfile%\Recent with %AppData%\Microsoft\Windows\Recent.
Related
I created a .bat that will copy pdf file in specified server. The folder structure of file is like this :
\\server\
'----010000-0000\
'----010000-0000-0705-1\
'-------010000-0000-DATA.pdf
'----010000-0000-0705-2\
'-------010000-0000-DATA.pdf
010000-0000 is the code that the user will input. Then batch will search with the exact folder with the code as filename and must copy the pdf file..
My problem is the folder before the pdf file is always has a 4 random numbers which is 0705 (as an example). What I was thinking is to pass through that folder what ever the 4 random numbers value will be. To copy the pdf file
for example (if would do a static code, it will be like this) :
set /p code=Input Control Number:
goto FILE
:FILE
set VER=0
:FILE1
set /a VER +=1
if exist "%fp%\%CODE%\%CODE%-0705-%VER%\%CODE%-DATA.pdf" goto FPCOPY
IF NOT EXIST ELSE GOTO FILE1
:FPCOPY
xcopy "%fp%\%CODE%\%CODE%-0705-%VER%\%CODE%-DATA.pdf" "%HOMEPATH%\Desktop\Backup\%CODE%\" /D /E /C /I /Y /H
start "" "%HOMEPATH%\Desktop\Backup\%code%"
It will surely copy the data. But as I said that 4 digit numbers is always random.
What I have tried is this one (I only replace the 4 digit no. by an *) :
set /p code=Input Control Number:
goto FILE
:FILE
set VER=0
:FILE1
set /a VER +=1
if exist "%fp%\%CODE%\%CODE%-*-%VER%\%CODE%-DATA.pdf" goto FPCOPY
IF NOT EXIST ELSE GOTO FILE1
:FPCOPY
xcopy "%fp%\%CODE%\%CODE%-*-%VER%\%CODE%-DATA.pdf" "%HOMEPATH%\Desktop\Backup\%CODE%\" /D /E /C /I /Y /H
start "" "%HOMEPATH%\Desktop\Backup\%code%"
This is assuming the later versioned directories are always created after the previous versions:
#echo off
set fp=<SET YOUR FILEPATH HERE>
set /p "code=Input Control Number: "
set cnt=0
:start
for /f "delims=" %%a in ('dir /b /s /ad /o-d "%fp%\%code%" ^| findstr "%code%" ^|more +%cnt%') do (
if exist "%%~a\%code%-DATA.pdf" (
set "found=%%~a"
goto :done
) else (
if %cnt% equ 10 goto :eof
set /a cnt+=1
goto start
)
)
:done
set cnt=
xcopy "%found%\%code%-DATA.pdf" "%USERPROFILE%\Desktop\Backup\%code%\" /D /E /C /I /Y /H
start "" "%USERPROFILE%\Desktop\Backup\%code%"
We really just check the latest folder for the file, if it does not exist, we skip that folder and try and older version until we find it, then copy the file.
As part of my job I need to remove files from computers that are being shipped over seas, this task is done several times a week and can be scripted, however I am terrible with script, I am in no means a coder of any sort.
My issue is that I need to clear out the C:\Users folder whilst keeping the Public and Default folders. I know how to remove a directory and all subfolders however I don't know how to keep just those two folders.
The code I have so far is:
#Echo off
color F
:Choice
Echo.
Echo.
set choice=
set /p choice="Type Y to proceed or N to close the tool and then press ENTER:----"
IF '%Choice%' =='Y' GOTO DELETE
IF '%Choice%' =='y' GOTO DELETE
IF '%Choice%' =='N' GOTO END
IF '%Choice%' =='n' GOTO END
:DELETE
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
Echo.
Echo.
#Echo All Files Removed
GOTO END
:END
echo.
echo.
Echo *****...Closing programme... Please wait...*****
Timeout /t 3
exit
Do you know if this is possible?
The following method, ran As Administrator, may work for you:
#Echo Off
Color 0F
Echo=&Echo=
Choice /N /M "Type Y to proceed or N to close the tool"
If ErrorLevel 2 Exit /B
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do WMIC UserAccount Where "SID='%%A' And LocalAccount='TRUE'" Delete
Echo=&Echo=&Echo All Files Removed&Echo=&Echo=
Echo *****...Closing programme... Please wait...*****
Timeout 3 /NoBreak>Nul
Exit /B
Special!='True' will ignore special accounts, which include Administrator, Public and Default.
I make a Script which deletes all empty Folders with Subfolders in a Path.
Now i have to make, if a folder was created 2 days ago and its empty it should be deleted with the other empty folders that are older than 2 Days.And if not it should be not deleted.
And i also need/want to make that deleted Folder are written in a Log.
I made that with the 5 Filetypes but i dont know how this schould work with the Folders.
Im really new to Batch, so i dont know what i should do.
I checked Google but the Results did not match with my problem.
I hope someone can help me.
Here is my Code that i´ve written so far:
#echo off
::start path / Variables
set startdir="C:\Users"
::Initialize the Variable
set /a loop=0
::Directory c:\temp\ will be created, if the folder not exists
if not exist c:\temp\ md c:\temp\
::Create Logfile for Deleted Filetypes in C:\Log\LOG_Useless_File_Killer.txt
echo ----------------------------------- >> C:\Log\LOG_Useless_File_Killer.txt
echo Logfile from: %date% at %time% >> C:\Log\LOG_Useless_File_Killer.txt
echo. >> C:\Log\LOG_Useless_File_Killer.txt
::this 5 Filetypes are going to be deleted immediately
del C:\Users\Thumbs.db /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\desktop.ini /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\*.DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\*._DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\*.desktop /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
::Writes the directorys in c:\temp\tmp.txt.
dir /AD /b /s %startdir% > c:\temp\tmp.txt
::at goto start it will be start again
:start
::the Variable %loop% is increased by 1
set /a loop =%loop%+1
::at 5 --> goto exit
if %loop%==5 goto exit
::Under 5 --> goto start
else goto start
::deletes every empty folder which is written in C:\temp\tmp.txt
for /F "delims=" %%i in (c:\temp\tmp.txt) do rd "%%i"
::--> goto start and begins again
goto start
::%loop% has reached 5 --> exit
:exit
::Console window will be closed
exit
pause
exit
Pradeep is absolutely right about the best way to go is for files.
For deleting folders, try this:
FORFILES -p "" /D -15 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
/D is for number of days, you can play with command parameters to meet exact requirement.
You can also use environment variables too so you can easily only delete files on the user that is currently logged on. For example, you can use %HOMEPATH%\Desktop to get to the desktop of the current user. More environment variables here.
I am trying to cache clear in Mozilla browser by following command
#echo off
set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles
del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"
for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do del /q /s /f %%x\*sqlite
It is working fine but how can i make sure that execution of the command is completed i.e. cache in mozilla browser is cleared.
Ok, so as Christian.K stated, you can use %ERRORLEVEL% to determine the success or failure of each command executed. Lasse V. Karlsen stated, you can fail on this %ERRORLEVEL%'s value. Example using your code below:
#echo off
set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles
del /q /s /f "%DataDir%"
if %ERRORLEVEL% NEQ 0 goto :failure
rd /s /q "%DataDir%"
if %ERRORLEVEL% NEQ 0 goto :failure
setlocal enabledelayedexpansion
for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do (
del /q /s /f %%x\*sqlite
if !ERRORLEVEL! NEQ 0 goto :failure
)
endlocal
goto :EOF
:failure
echo do something here
exit
goto :EOF
Use NEQ 0 for testing %ERRORLEVEL%. I have seen this value be something other than 1 when a command fails.
setlocal enabledelayedexpansion handles the %ERRORLEVEL% variable changing after each execution of the for loop. Without this, it would just evaluate to whatever was contained in the %ERRORLEVEL% variable when the for loop was entered.
goto :EOF at the end of your main script so your labels are not executed after the script completes. Also add this at the end of your label to prevent the same (the exit I have in mine would prevent that as well, but you may not want to exit the script on failure).
To handle spaces etc in usernames you can use this as your last command:
for /d %%x in ("C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*") do del /q /s /f "%%x\*.sqlite"
This also assumes that the files you are deleting have a .sqlite extension.
The files will probably be locked if they are in use, if the browser hasn't been closed.
My working batch file scans a long list of remote servers, copies anything there to a local server, checks the log file for a keyword, and if the keyword is found sends an email. I noticed it is always sending emails, even with a blank log file.
I discovered both FOR loops are using the %1 variable for their output - as seen in ECHO %1 and each line of the called :servermove. For lack of a better explanation it is not resetting %1 to null between loops.
I reviewed almost a dozen SO posts and am somewhat confident using SETLOCAL ENABLEDELAYEDEXPANSION would resolve this. That is where my understanding ends and I am unsuccessful thus far.
Here is the relevant code:
SET DATE=%date:~4,2%-%date:~7,2%-%date:~10,4%
SET HH=%time:~0,2%
SET MN=%time:~3,2%
SET TSTAMP=Time Run is %HH%%MN%
SET DATETIME=%DATE% at %HH%%MN%
SET LOGFILE="\\nt980a3\CreditFileImagesTransmission\LogFiles\%DATETIME%-File Move Log.txt"
SET MailDst=
SET MailSrc=
SET MailSrcName=Center to LDSD File Mover
SET OKMailSub=A Branch Has Sent You Some Files
ECHO %DATETIME% > %LOGFILE%
ECHO. >> %LOGFILE%
FOR /F "tokens=1" %%A IN (%~dp0SourceServers.txt) DO CALL :ServerMove %%A
:cleanuplogs
PUSHD "\\nt980a3\CreditFileImagesTransmission\LogFiles" &&(
FORFILES /S /M *.txt /D -45 /C "CMD /C DEL /Q #path"
) & POPD
:mailtest
FOR /F "tokens=*" %%A IN (%LOGFILE%) DO CALL :searchlog "%%A"
:searchlog
ECHO %1 | find "\\nt">NUL
IF NOT ERRORLEVEL 1 GOTO successmail
GOTO exit
:successmail
IF EXIST %temp%\to.txt DEL %temp%\to.txt
FOR %%a IN (%MailDst%) DO ECHO %%a>>%temp%\to.txt
"%~dp0sendmail.exe" /TO=%temp%\to.txt /FROM=%MailSrcName% ^<%MailSrc%^> /REF=%OKMailSub% /MESSAGE=%LOGFILE% /HOST=
:exit
EXIT
:ServerMove
DIR /S /B \\%1\CreditFileImagesTransmission\*.* >> %LOGFILE%
XCOPY /E /C /I /Y "\\%1\CreditFileImagesTransmission\*.*" "\\nt980a3\CreditFileImagesTransmission\%DATE%\%HH%%MN%\"
FOR /D %%P IN ("\\%1\CreditFileImagesTransmission\*.*") DO RMDIR "%%P" /Q /S
DEL /Q /S "\\%1\CreditFileImagesTransmission\*.*"
I tried changing :mailtest to use %%B in both instances but that also fails. Placing SETLOCAL ENABLEDELAYEDEXPANSION and its counterpart ENDLOCAL before one or the other loop and changing the %%A to !A! does not work either.
Would someone kindly point out the error in my ways and offer suggestions or resources that will help me resolve this?
%1 is the first parameter provided to the procedure - either from the command-line (in the main procedure) or the parameter following the procedure name in call :procedurename parameter1.
In your case, %1 to :servermove is an entry from SourceServers.txt and %1 to :searchlog is each line from %LOGFILE%.
Since you've censored your batch, what you've posted makes little sense. For instance, the :searchlogs routine will take the first line from %LOGFILE% and go to successmail or cleanlogs depending on whether that first line contains the target string \\nt. What it does from there, we can't tell.
We're faced with an XY problem - trying to fix a solution, not a problem.
First problem: Don't use date as a user-variable. It's a "magic variable" which contains the date but it's overridden by a specific set statement.
Having run :servermove for each entry in SourceServers.txt, you are
- accumulating a directory list from \CreditFileImagesTransmission\*.* on that server.
- copying those files to server nt980a3 with a date/timestamp but not including the source-servername so any duplicate name anywhere will overwrite an earlier version. I suggest you include %1 into your destination-name.
- deleting subdirectories
- deleting files.
I'd suggest you simply remove the directory \\%1\CreditFileImagesTransmission\, then re-create it.
I'd also suggest that you add an extra line
goto :eof
after the del /q /s... line. This will cause execution to be transferred to the end-of-file (the colon in :eof is required) and may seem superfluous, but it ensures that the routine has a defined endpoint - if you add a further routine, there is no way the :servermove routine will continue into your new code.
After each server has been processed, you proceed to the :cleanuplogs routine, which I presume deletes logs older than 45 days.
Your next statement is a real problem. What it will do is grab the very first line of the logfile (which contains "%DATE% at %HH%%MN%" with the date resolved as you've set at the start and it then processes this line in :searchlog; there is no \\nt in this line, so errorlevel is set to 1, and the batch proceeds to :EXIT (not a good label in my view, since it's a keyword); executes an exitand should terminate the batch.
This appears not to be what it is actually doing, and I'm at a loss to explain why.
I'd suggest changing
:mailtest
FOR /F "tokens=*" %%A IN (%LOGFILE%) DO CALL :searchlog "%%A"
:searchlog
ECHO %1 | find "\\nt">NUL
IF NOT ERRORLEVEL 1 GOTO successmail
GOTO exit
to
:mailtest
find "\\nt" %LOGFILE%>NUL
IF NOT ERRORLEVEL 1 GOTO successmail
:failmail
echo "\\nt" was found in the log
pause
GOTO exit
but I can't test that...
:mailtest
FOR /F "tokens=*" %%A IN (%LOGFILE%) DO CALL :searchlog "%%A"
You are missing a GOTO :EOF or similar goto here because it will drop through to the routine below once the above is finished.
:searchlog
ECHO %1 | find "\\nt">NUL
IF NOT ERRORLEVEL 1 GOTO successmail
GOTO exit
I feel you can not carry the %1 of first for loop to others. Try to transfer that to another variable like below.
:ServerMove
set servername=%1
DIR /S /B \\%servername%\CreditFileImagesTransmission\*.* >> %LOGFILE%
XCOPY /E /C /I /Y "\\%servername%\CreditFileImagesTransmission\*.*" "\\nt980a3\CreditFileImagesTransmission\%DATE%\%HH%%MN%\"
FOR /D %%P IN ("\\%servername%\CreditFileImagesTransmission\*.*") DO RMDIR "%%P" /Q /S
DEL /Q /S "\\%servername%\CreditFileImagesTransmission\*.*"
Cheers, G