Batch Coding - Choose command not returning the correct answer - batch-file

I am trying to create a batch file which has two options:
Create a series of folders with custom names (defined by variables inputted by the user)
OR
Create the same as before except also, once completed, automatically
copying all appropriate files to the appropriate folders in the
structure.
The idea is that with this batch file I could keep my film projects (As I am a freelance filmmaker working with my brother) in exactly the same folder structures across all 3 of our computers. AKA a uniform Project folder structure can be implemented with a simple double click and a few variables to fill in, while in the case of option 2 any files found on a specified SD Card are also moved to the appropriate folders within the structure.
The actual xcopy commands and mkdir commands etc. work exactly how they are supposed to.
Since the different computers will be allocating different drive letters depending on what is already attached to the computer before entering the SD Card into the card reader, I will never know what the drive letter of the SD Card will be (from which the footage and images will be loaded onto the computers).
For example:
One computer could have an external hard drive in (of which we have a few) at the time of the SD Card being plugged in, while another may have only the SD Card as an external device.
The code below is the complete .bat file including past code which I had tried but didn't work or needed to be excluded for some reason. I have some lines of code in comments so I know what I had tried in the past, these are obviously not meant to be used anymore.
:Menu
ECHO OFF
CLS
ECHO OFF
TITLE Saborknight Productions - Project Creator
ECHO.
ECHO ...............................................
ECHO PRESS the appropriate number to select your task, or 3 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Create Saborknight Default folder Hierarchy within a project folder
::(Disabled)ECHO 2 - Create Saborknight Default folder Hierarchy AND copy ALL files from SD Card to the appropriate folder
ECHO 3 - EXIT
ECHO.
ECHO WARNING - Option 2 will delete the files from the Recording Device automatically. Secure the device before proceeding!
ECHO.
ECHO.
CHOICE /c 123 /n
IF ERRORLEVEL 3 EXIT
IF ERRORLEVEL 2 GOTO :CREATE
IF ERRORLEVEL 1 GOTO :CREATE
::End of Menu
---------------------------------------------------------------------------------------------
:CREATE
::Creates DIR Folder Structure as per Saborknight Productions Standard
ECHO OFF
CLS
::Variables Descriptor
SET /P project_name=Enter the Project Name: || SET project_name=NothingChosen
If "%project_name%"=="NothingChosen" (
GOTO ERROR_PROJECT
) ELSE (
GOTO CREATOR
)
:ERROR_PROJECT
::If no Project Name is entered
ECHO Please enter the Project Name
GOTO CREATE
:CREATOR
::DIR Creator + Confirmation
::Videos DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Footage
::Cut from above MKDIR -> \"Day 1"\%device_name%
::Images DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Images\%device_name%
::Audio DIR -> Soundtracks
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Soundtracks
::Audio DIR -> Recordings
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Recordings
::Audio DIR -> Sound FX
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\"Sound FX"
::Premier Project DIR
ECHO OFF
MKDIR D:\Dropbox\"Project Files"\"%project_name%"
ECHO The Folder Structure has been created
ECHO.
ECHO.
ECHO What would you like to do next?
Echo.
ECHO 1 - View Created Folder Structure
ECHO 2 - Go to Menu
ECHO 3 - EXIT
::Choice is invisible
CHOICE /c 123 /n
IF ERRORLEVEL 3 EXIT
IF ERRORLEVEL 2 GOTO MENU
IF ERRORLEVEL 1 GOTO VIEW
::End of CREATE
---------------------------------------------------------------------------------------------
:COPY
::Creates DIR Folder Structure as per Saborknight Productions Standard + Copying/Moving Files from a selected SD Card connected to the computer
ECHO OFF
CLS
:COPY_PROJECT
::Project Variable Descriptor
SET /P project_name=Enter the Project Name: || SET project_name=NothingChosen
If "%project_name%"=="NothingChosen" (
GOTO ERROR_COPY_PROJECT
) ELSE (
GOTO COPY_DEVICE_NAME
)
::If True, Process continues
:COPY_DEVICE_NAME
::Device Name Variable Descriptor
ECHO.
SET /P device_name=Enter the Name of the Recording Device: || SET device_name=NothingChosen
If "%device_name%"=="NothingChosen" (GOTO DAMN_DEVICE) ELSE (GOTO DEVICE_DIR)
:DEVICE_DIR
::Device DIR Variable Descriptor
ECHO.
::Selecting the location of the SD Card of the Recording Device + Confirmation
ECHO Copy files from drive: E, F, G, H or I?
SET _drive=G
::SETLOCAL EnableDelayedExpansion
::CHOICE /c fgehi
::IF ERRORLEVEL 5 SET _drive=I
::IF ERRORLEVEL 4 SET _drive=H
::IF ERRORLEVEL 3 SET _drive=G
::IF ERRORLEVEL 2 SET _drive=F
::IF ERRORLEVEL 1 SET _drive=E
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
::DIR Creator + Confirmation -> Only if Device Drive is successfully selected
::Videos DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"
::Images DIR
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Images\"%device_name%"
::Audio DIR -> Soundtracks
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Soundtracks
::Audio DIR -> Recordings
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\Recordings
::Audio DIR -> Sound FX
ECHO OFF
MKDIR D:\"Movie Projects"\"%project_name%"\Audio\"Sound FX"
::Premier Project DIR
::ECHO OFF
::(Disabled for Testing)MKDIR D:\Dropbox\"Project Files"\"%project_name%"
ECHO.
CHOICE /c YN /m "Would you like files to be deleted automatically from the device? Press Y=Yes or N=No"
IF ERRORLEVEL 2 GOTO NO_DELETE
IF ERRORLEVEL 1 GOTO DELETE
:NO_DELETE
::No Deletion once Copied from Device
::For Copying Videos
ECHO.
ECHO Copying Videos
XCOPY %_drive%:\DCIM\ "D:\Movie Projects\"%project_name%"\Footage\"Day 1"\"%device_name%"\" *.MOV *.mp4 /V /-Y
::COPY %_drive%:\DCIM\ "D:\Movie Projects\"%project_name%"\Footage\"Day 1"\"%device_name%"\" *.MOV *.mp4
::ROBOCOPY %_drive%:\DCIM "D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"\*" *.mov *.mp4 /mir /copy:DATO /np /eta /log+:"Log - Footage CopyOnly.txt" /nosd /nodd
PAUSE
::For Copying Images
ECHO.
ECHO Copying Images
XCOPY %_drive%:\DCIM\ "D:\Movie Projects\"%project_name%"\Images\"%device_name%"\" *.MOV *.mp4 /V /-Y
::COPY %_drive%:\DCIM\*.jpg;*.png;*.cr2 "D:\Movie Projects\"%project_name%"\Images\"%device_name%"\" /V /-Y
::ROBOCOPY %_drive%:\DCIM "D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"" *.jpg *.png *.cr2 /copy:DATO /mir /np /eta /log+:"Log - Images Copy Only.txt" /nosd /nodd
PAUSE
GOTO SUCCESS_MSG
:DELETE
::Copying with Automatic Deletion once Copied
::For Copying and Deleting Videos
ECHO Copying and Deleting Videos
MOVE %_drive%:\DCIM\*.mov;*.mp4 D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%"\ /-Y
::ROBOCOPY %_drive%:\DCIM D:\"Movie Projects"\"%project_name%"\Footage\"Day 1"\"%device_name%" *.mov *.mp4 /mov /copy:DATO /np /eta /log+:"Log - Footage Copy Delete.txt" /nosd /nodd
PAUSE
::For Copying and Deleting Images
ECHO Copying and Deleting Images
MOVE %_drive%:\DCIM\*.jpg;*.png;*.cr2 D:\"Movie Projects"\"%project_name%"\Images\"%device_name%"\ /-Y
::ROBOCOPY %_drive%:\DCIM D:\"Movie Projects"\"%project_name%"\Images\"%device_name%" *.jpg *.png *.cr2 /mov /copy:DATO /np /eta /log+:"Log - Images Copy Delete.txt" /nosd /nodd
PAUSE
GOTO SUCCESS_MSG
:SUCCESS_MSG
::Only if all Copying/Moving and DIR Creation Processes are Completed Successfully
ECHO.
ECHO The Folder Structure has been created and files copied/moved
ECHO.
ECHO.
ECHO What would you like to do next?
Echo.
ECHO 1 - View Created Folder Structure
ECHO 2 - Go to Menu
ECHO 3 - EXIT
::Choice is invisible
CHOICE /c 123 /n
IF ERRORLEVEL 3 EXIT
IF ERRORLEVEL 2 GOTO MENU
IF ERRORLEVEL 1 GOTO :VIEW
:DAMN_DEVICE
ECHO.
ECHO Please enter the Device Name, Re-entering the Project Name
ECHO.
GOTO COPY
:ERROR_COPY_PROJECT
::If no Project Name is entered
ECHO.
ECHO Please enter the Project Name
ECHO.
GOTO COPY_PROJECT
ENDLOCAL
::End Of COPY
---------------------------------------------------------------------------------------------
:VIEW
ECHO OFF
CLS
TREE D:\"Movie Projects"\"%project_name%"
ECHo.
ECHO Press any key to return to Menu
ECHO.
PAUSE
GOTO MENU
::End of VIEW
The Problem
In this part of the code, the CHOOSE command wont choose the correct choice:
#ECHO OFF
CHOICE /c fgehi
IF ERRORLEVEL 5 SET _drive=I
IF ERRORLEVEL 4 SET _drive=H
IF ERRORLEVEL 3 SET _drive=E
IF ERRORLEVEL 2 SET _drive=G
IF ERRORLEVEL 1 SET _drive=F
Pause
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
The Evidence of the Problem being a problem
I tested the Batch file on two separate machines (One by remote access so that the two screens lay side by side) and can be viewed here: Image of the Problem being a problem...
Apologies for the dirtiest code in history with a complete overuse of comments. This was supposed to be a one night project which has turned into a month long project and I still haven't solved it!
Even a programming friend of mine cannot explain why it wont work.
All help much appreciated in advance!!!
Also any alternative suggestions to complete the described tasks above are definitely welcome, I love learning new code!
Saborknight

Your program fail because IF ERRORLEVEL n ... "execute the command if the errorlevel is GREATER OR EQUAL than the number given", that is, you placed the IF commands in the opposite order. This works correctly:
#ECHO OFF
CHOICE /c fgehi
IF ERRORLEVEL 1 SET _drive=F
IF ERRORLEVEL 2 SET _drive=G
IF ERRORLEVEL 3 SET _drive=E
IF ERRORLEVEL 4 SET _drive=H
IF ERRORLEVEL 5 SET _drive=I
Pause
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
May I suggest you a different, simpler method?
#ECHO OFF
SET option=0FGEHI
CHOICE /c fgehi
CALL SET _drive=%%option:~%ERRORLEVEL%,1%%
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause

As documented here: http://www.robvanderwoude.com/choice.php
By the way, in Windows NT 4 this won't work, since the SET command itself will set an errorlevel (usually 0)!
However, Windows NT makes it easy by storing the latest errorlevel in the environment variable ERRORLEVEL
This works for me:
#ECHO OFF
CHOICE /c fgehi
IF %ERRORLEVEL% EQU 5 SET _drive=I
IF %ERRORLEVEL% EQU 4 SET _drive=H
IF %ERRORLEVEL% EQU 3 SET _drive=E
IF %ERRORLEVEL% EQU 2 SET _drive=G
IF %ERRORLEVEL% EQU 1 SET _drive=F
Pause
ECHO.
ECHO You have selected drive %_drive%:
ECHO.
Pause
Unless you know the proper ritual to ask the Microsoft gods for forgiveness, I would recommend you stay away from batch, it is a lot of trouble.
Even PowerShell is much less of a hassle.

Related

Batch, making a .ini file to read from [duplicate]

This question already has answers here:
Shortest Windows batch file code to get first line of a file set to a variable?
(6 answers)
Closed 1 year ago.
I'm trying to make a Borderlands 3 .bat file to remove certain files (it's a no-intro script). I do want to store the path the user input so the next time the game updates and re-download these files you can remove them fast without asking for a path later on.
This is the first code I have:
set BL3=C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3
set /p BL3= Where's the Borderlands 3 folder? (Default: C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3) =
The full code is here:
#echo off
title Borderlands 3 No Intro Script
:inicreator
set BL3=C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3
set /p BL3= Where's the Borderlands 3 folder? (Default: C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3) =
set errorlevel=0
choice /C YN /N /M "Remove the loading screen as well? (Y = Yes | N = No)"
echo If you haven't cached shaders the screen will be
echo black for awhile if you delete the loading screen.
if '%errorlevel%'=='1' (set LD=yes)
if '%errorlevel%'=='2' (set LD=no)
echo.
echo Creating .ini to store the path for future use ...
echo %BL3% >"bl3settings.ini"
if exist "%BL3%\OakGame\Content\Movies\2KLOGO.mp4" del "%BL3%\OakGame\Content\Movies\2KLOGO.mp4"
if exist "%BL3%\OakGame\Content\Movies\AMDLOGO.mp4" del "%BL3%\OakGame\Content\Movies\AMDLOGO.mp4"
if exist "%BL3%\OakGame\Content\Movies\GBXLOGO.mp4" del "%BL3%\OakGame\Content\Movies\GBXLOGO.mp4"
if '%LD%'=='yes' (if exist "%BL3%\OakGame\Content\Movies\Loading.mp4" del "%BL3%\OakGame\Content\Movies\Loading.mp4")
if '%LD%'=='yes' (echo All done! Removed 2KLOGO.mp4, AMDLOGO.mp4, GBXLOGO.mp4 and Loading.mp4)
if '%LD%'=='no' (echo All done! Removed 2KLOGO.mp4, AMDLOGO.mp4 and GBXLOGO.mp4)
echo These will come back after Boderlands 3 updates.
timeout /t 2 /nobreak>nul
The goal is to write the current loation to a file, then read it and make a variable that we can use...
Using the idea mentioned in SomethingDark's comment, here's a quick untested rewrite of my undertsanding of your script:
#Echo Off
SetLocal EnableExtensions
Title Borderlands 3 - No Intro Script
CD /D "%~dp0" 2>NUL || Exit /B
:DefaultLocation
Set "BL3="
If Exist "%~dp0bl3settings.ini" Set /P "BL3=" 0<"%~dp0bl3settings.ini" 2>NUL
If Not Defined BL3 Set "BL3=%ProgramFiles(x86)%\Steam\steamapps\common\Borderlands 3"
PushD "%BL3%\OakGame\Content\Movies" 2>NUL
If Not ErrorLevel 1 GoTo Actions
:GetLocation
Set "BL3="
Set /P "BL3=Where is the Borderlands 3 directory location?>"
If Not Defined BL3 GoTo GetLocation
PushD "%BL3:"=%\OakGame\Content\Movies" 2>NUL
If ErrorLevel 1 GoTo GetLocation
:Actions
Echo(
Echo Creating .ini to store the path for future use ...
CD 1>"%~dp0bl3settings.ini"
Set "StandardList="2KLOGO.mp4", "AMDLOGO.mp4", "GBXLOGO.mp4""
Set "ExtendedList=%StandardList%, "Loading.mp4""
"%SystemRoot%\System32\choice.exe /M "Remove the loading screen as well"
If ErrorLevel 2 (Del /A /F %StandardList% 2>NUL
Echo All done. Removed: %StandardList%
GoTo Finish
)
Echo If you have not cached shaders, the screen will be black for a while...
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
Del /A /F %ExtendedList% 2>NUL
Echo All done. Removed: %ExtendedList%
:Finish
PopD
Echo These files will come back after Borderlands 3 updates.
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL

Batch script to check if a folder exists. If yes, then copy a file from another folder to this folder

I am new to programming, with no background whatsoever.
I am creating a batch script which will create folders based on user input.
Then it will copy files from a shared server to the local folders that have been created.
I have the following script:
#ECHO OFF
REM *********************************************************************
REM **This will create directories and Copy Files for your project**
REM *********************************************************************
ECHO Plese enter the no. between 1 to 5 only
set /p input=[Please enter:1-Belgium, 2-Austria, 3-France, 4-Germany, 5-Greece]
if %input%==1 md BEL
if %input%==2 md AUT
if %input%==3 md FRA
if %input%==4 md DEU
if %input%==5 md GRE
if %input%==1 cd BEL
if %input%==2 cd AUT
if %input%==3 cd FRA
if %input%==4 cd DEU
if %input%==5 cd GRE
::ECHO Please enter the no. between 1 to 5 only
:END
set /p userinput=[Please enter your folder name]
mkdir %userinput%
cd %userinput%
set /p input=[Press 1 for 1.0, 2 for 2.0]
if %input%==1 md Folder1
if %input%==2 md Folder2
IF EXIST .\Folder1 xcopy "C:\ProgramFiles\kasras01\file2.txt"
All along, the code does well, until it comes to the part where I need to check if a folder exists. If it does, then I need to copy a file from another location to this folder.
But I am unable to get it to work. I think my syntax is incorrect.
How do I get it to work?
Grateful for any help.
The bottom line of your posted script should look more like this:
IF EXIST "Folder1" XCOPY "C:\ProgramFiles\kasras01\file2.txt" "Folder1\" /Y
Here also is a rewritten example using both Choice for the controlled input and Set /P with checks for the uncontrolled input:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
CD /D "%~dp0" 2>Nul || Exit /B
REM **************************************************************************
REM ** This will create directories and copy a single file for your project **
REM **************************************************************************
Echo 1. Belgium&Echo 2. Austria&Echo 3. France&Echo 4. Germany&Echo 5. Greece
Choice /C 12345 /M "Which one"
If ErrorLevel 5 MD "GRE" 2>Nul & CD "GRE"
If ErrorLevel 4 MD "DEU" 2>Nul & CD "DEU"
If ErrorLevel 3 MD "FRA" 2>Nul & CD "FRA"
If ErrorLevel 2 MD "AUT" 2>Nul & CD "AUT"
If ErrorLevel 1 MD "BEL" 2>Nul & CD "BEL"
:DirPick
Echo(&Echo Please type your chosen directory name
Set /P "UserInput= then press enter: "
If Not Defined UserInput Echo Empty names are disallowed& GoTo DirPick
SetLocal EnableDelayedExpansion
For /F Tokens^=2^ Delims^=^\^/^:^*^?^"^|^<^>^ EOL^= %%A In ("[!UserInput!]"
) Do Echo Disallowed character detected& Set "UserInput=" & GoTo DirPick
EndLocal & MD "%UserInput%" 2>Nul & CD "%UserInput%"
Choice /C 12 /N /M "Press 1 for 1.0, 2 for 2.0"
XCopy "C:\ProgramFiles\kasras01\file2.txt" "Folder%ErrorLevel%\" /Y>Nul
The script is currently designed to use its own location as the base for the new directories etc. If you wish for a different base directory, change %~dp0 only on line 3 to the path you require.The XCopy command at the bottom currently has no checking mechanism, this means that if "C:\ProgramFiles\kasras01\file2.txt" doesn't exist, Folder[1|2] will not be created either! (Otherwise it would create Folder[1|2] and copy "C:\ProgramFiles\kasras01\file2.txt" into it.)
IF EXIST .\Folder1\ xcopy ...
Your code creates (eg) folder1 in the current directory, then looks for folder1 in the parent directory. Adding the \ looks for a directory only, not a file.
Are you aware that the xcopy will copy the file into the current directory, not the folder1 subdirectory?
xcopy "C:\ProgramFiles\kasras01\file2.txt" ".\folder%input%\"
should create and copy. You'd no doubt want to validate input is 1 or 2 first.
I need to check if a folder OR a file exists in a directory and I need to distinguish between them.
I found using \nul to be unreliable in some cases (Windows 10) - meaning that checking IF EXIST "%foldername%\nul" returned true regardless if there was a file or folder with the same name.
I found out that in my case, using IF EXIST "%foldername%\" (without nul) worked fine.
Snippet:
IF EXIST "%fname%" (
goto :f1
) ELSE (
goto :f2
)
:f1
IF EXIST "%fname%\" (
echo Folder with the same name already exists!
echo Cannot continue!
) ELSE (
echo File with the same name already exists!
echo Cannot continue!
)

Batch file over network path

I have the following batch script to make life easier at work.
Here is what it is supposed to work:
1- Drag and drop some file onto the .bat
2- Choose file's destination on the "menu"
3- Script copy's files to destination folder
4- Script executes remote procedure (that's the PSexec line)
5- Script copy's the result of the remote procedure to other folders.
And this works fine... except for a "small" detail with which i need some help.
When i try to copy the network location \10.250.39.116\d%... if i haven't previously logged into that machine it wont work.
I've been looking into the 'net use' command to overcome this, but i'm not sure if it suits my needs.
There are a total of 4 different machines i need to authenticate, dependent on the choice of the menu.
Actual Question:
Can i log in to such machines with the batch, and avoid creating duplicate connections every time i run the script ? If so, how?
Thank you for your time!
:)
I know the paths I have all point to the same place :)
#echo off
setlocal enabledelayedexpansion
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Compilar em Qualidade
ECHO 2 - Compilar na HSDEV
ECHO 3 - Compilar nas DEMOS
ECHO 4 - EXIT
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO :QUAL
IF %M%==2 GOTO :HSDEV
IF %M%==3 GOTO :DEMO
IF %M%==4 GOTO EOF
:QUAL
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:HSDEV
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:DEMO
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:PROCESSA
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~nx"
set "pathVec[!argCount!]=%%~dpx"
)
rem echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do (
echo Vou compilar %%i - "!argVec[%%i]!"
if exist %PathForms6%!argVec[%%i]!.* del /q %PathForms6%!argVec[%%i]!.*
if exist %PathForms10%!argVec[%%i]!.* del /q %PathForms10%!argVec[%%i]!.*
robocopy "!pathVec[%%i]!." %PathForms6% !argVec[%%i]!.fmb > nul
)
c:
cd c:\pstools
psexec %PathCompilador%
for /L %%i in (1,1,%argCount%) do (
if exist "%PathForms10%!argVec[%%i]!.fmx" (
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPriv% /y
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPub% /y)
)
pause
How much testing have you done with net use? Try running it twice at the command line. Notice how the output changes at the second running:
As you can see, where a connection has already been established, net use will output a summary of the connection rather than creating a duplicate connection.
If you prefer, you could use conditional execution or errorlevel checking. Using this method, you can avoid calling net use until xcopy fails, which should only be the first time. Here's a short example, simply to illustrate the mechanics:
#echo off
setlocal
ping -n 1 10.250.39.116 | find /i "TTL=" >NUL || (
echo 10.250.39.116 is offline. Unable to continue. Press any key to exit.
pause >NUL
goto :EOF
)
call :xcopy "%~1" "destination"
echo Press any key to exit.
pause >NUL
net use \\10.250.39.116\d$ /delete >NUL 2>NUL
goto :EOF
:xcopy <source> <dest_dir>
xcopy /L "%~1" "%~2" 2>NUL || (
net use \\10.250.39.116\d$ /user:username password >NUL 2>NUL
xcopy /L "%~1" "%~2"
)
goto :EOF

How do I prompt users to insert Drive letter in Batch-File file?

I have a working simple batch-file program which unhides predefined drive hidden folders.
What I want to do is:
To insert Drive letter by a user instead of predefined and other option if you suggested me.
my unhide.bat code is:
#echo off
D:
attrib -r -s -h /s /d
pause
You can use set /p DriveLetter=Please enter drive letter.. If you then run %DriveLetter% as a command, it will expand as you expect.
To make your code work:
#echo off
set /p DriveLetter=Please enter drive letter.
%DriveLetter%
attrib -r -s -h /s /d
pause
They would enter "D:" and it would accept it as such.
The only thing to note is that if someone uses an incorrect drive letter, bad stuff will happen.
Alternatively, you could use a large block with CHOICE, but that seems overkill. You'd have to define every drive letter. Thus, I won't go into detail on this.
This should do:
SET /P letter=Please give your drive letter and press ENTER:
ECHO %letter%
PAUSE
I guess that you try to unhide the folders into the USB Drive caused by a worm
If you like to detect the drive letter of your USB Key, Try this code instead,
I made this tool to unhide folders and files and delete all infected links into the USB key
#echo off
Mode con cols=98 lines=10 & Color 9E
Title Searching the Drive letter of your USB Key by Hackoo 2014
echo.
ECHO *******************************************************************************************
echo.
echo Searching the drive letter of your USB Key .......
echo.
ECHO *******************************************************************************************
wmic logicaldisk get DeviceID,DriveType /Format:CSV > %Tmp%\tmp.txt
for /f "skip=2 tokens=1-3 delims=," %%a in ('%COMSPEC% /a /c type "%Tmp%\tmp.txt"') do echo %%b %%c >> %Tmp%\tmp2.txt
for /f "tokens=1" %%i in ('%COMSPEC% /a /c type "%Tmp%\tmp2.txt" ^|Find "2"') Do (set MyUSBDrive=%%i)
Del %Tmp%\tmp.txt & Del %Tmp%\tmp2.txt
cls
echo.
ECHO *******************************************************************************************
echo.
echo The drive letter of your USB Key is %MyUSBDrive%
echo.
ECHO *******************************************************************************************
pause
cls
echo.
echo.
ECHO *******************************************************************************************
echo.
echo Press a button to delete infected shortcuts and restore hidden files in your USB key
echo.
ECHO *******************************************************************************************
pause>nul
cls
echo.
echo.
ECHO *******************************************************************************************
echo.
echo Deleting infected shortcuts and restoring hidden files
echo.
ECHO *******************************************************************************************
:: To Unhide Folders and files into your USB key
Attrib -s -h -r %MyUSBDrive%\*.* /D /S >nul 2>&1
:: To delete all infected files.lnk
Del %MyUSBDrive%\*.lnk >nul 2>&1
Explorer %MyUSBDrive%
pause

Creating a Batch to go through all folders on a share and move files

Here is what I am attempting to do and if I can get your help it would be greatly appreciated!
Issue:
I want to create a script to do the following:
Go out to a network share where all account folders exist (2,400 folders with a 9-digit naming convention for account numbers, i.e folder name is 123456789).
Then go through a specific directory within the account folders that have a time stamp naming convention (i.e 123456789\Jan_29_2013_453pm).
Next, all folder structures are the same for all account folders EXCEPT the time stamped folder under the root (i.e 123456789`Jan_29_2013_453pm`\data).
Once being able to get through the time stamp folder there will be a set of folders that start off with a naming convention of media1, media2, media3, media4, media5, etc. that I need to access in order to move files.
I need to move a certain file from all media folders into media1. **There is an unknown amount of media folders for different account folders. Some may have 1 and other may have 50.
I need to run a test within the data folder (123456789\Jan_29_2013_453pm\data) to open a logfile.txt to see if a specific string exists at the end of the log (i.e item was successfully created as well as item has failed`).
If there is a SUCCESSFUL string in the logfile.txt, then go ahead and move all files from other media folders into media1. If a FAILURE string, then output the account folder name to a log file (output.txt) so I am aware which account folders failed (I would append the output as I will run this script multiple times).
The idea is to disregard account folders that have just a single media1 folder. Other account directories that have more than ONE media folder need to run a test against the log for SUCCESS or FAIL and depending on the string, to move a single file from all other media (media2,media3,media4,etc.) folders over to media1 unless FAILURE is read in the log.
I created a text file with all account folders on the share. I'm sure a FOR statement can be used against it. I'm also sure that a FOR statement can be used to go through the log in the "DATA" directory to look for the "SUCCESSFUL" or "FAILED" string.
The hard part I see is being able to get through the time stamped folder in order to get to the DATA directory.
THANKS AGAIN FOR ALL THE HELP!
#echo off
setlocal enabledelayedexpansion
:beginning
echo.
echo.
echo ===========================================================
echo Starting on !date! !time! on !Computername!
echo ===========================================================
echo.
echo Press ENTER to begin...
pause
echo.
goto :main
:main
cls
echo.
set /P acct=Please type the 9 digit account number you would like to restore:
set acctDir=X:\!acct!\
set acctDir2=media1\Setup\setup.exe /cd
set acctDir3=media1
set x=x:\!acct!\!userDir!
set sumLog="x:\!acct!\!userDir!\SummaryLog.txt"
set succ=finished
set log=c:\logs.txt
echo. Starting on !date! !time! on !Computername! >> !log!
echo.
echo The account number you selected is: !acct!
echo.
goto :user
:user
set /p answer=Is this correct (Y/N)?
echo.
if /i !answer!==y goto :yes (
) else (
echo.
echo Ok. Let's try again^^!
echo.
pause
cls
goto :main
)
)
:yes
set c=0
For /f %%a in ('dir !acctDir! /B /A:D') do (
set /a c+=1
echo !c! %%a
set dir!c!=%%a
)
echo.
set /p userIn="Select a directory [1-!c!]: "
set userDir=!dir%userIn%!
echo.
echo You selected !userDir! for your data retrieval.
echo.
goto :execute
:execute
echo.
echo The software will now be installed...
start !acctdir!\!userDir!\!acctDir2! >>!log!
:move
forfiles /p "!acctdir!\!userDir!\" /s /m *.ARC /c "cmd /c move #file !acctdir!\!userDir!\!acctdir3!"
goto
:string
findstr " .*!succ!" !sumLog!
if exist errorlevel 0 (
pause
goto :move
) else (
goto :eof
)
endlocal
goto :eof

Resources