Batch-Scripting permission changes for a copied file and subroutine possibilities? - batch-file

I am trying to achieve the following:
For every file in the source folder %1; Display it's details, ask the user whether the file should be copied and if the user answers "no" display a message "skipped", otherwise copy the file to the target folder %2, set the copy's permissions to "read-only", and display a message.
This is what I have managed so far but the last part I have had no luck understanding.
#echo off
rem if the source folder does not exist, display a message and exit
if exist "%1%" (
echo .
) else (
echo Source folder doesn't exist.
exit /b
)
if exist "%2%" (
echo Directory exists
) else (
md %2
echo Directory Created
)
FOR /F "DELIMS==" %%f in ('DIR "%1" /B') DO (
ECHO %%f
set p = NULL
SET /p p="Copy(y/n)?"
IF "%p%" == "y" (
COPY "%1\%%f" "%2"
ECHO Copied %%f to %2
) ELSE (
ECHO "%%f" Skipped
)
)
Is this possible? And if it is can it be done with a subroutine for the iteration process (copying and setting permissions)?

I made a batch script to do this operation
#echo off
::This batch file works with arguments or no
::copysec "sourcefolder" "destination folder"
if not "%1" == "" (
set "source=%1") else (
set /p "source=Source folder: ")
if not "%2" == "" (
set "destination=%2") else (
set /p "destination=Destination folder: ")
if not exist "%destination%" (
echo/Destination folder %source% not found&pause>nul&exit/b)
if exist "%source%" (
pushd "%source%") else (
echo/Source folder %source% not found&pause>nul&exit/b)
set /a count=0
for %%f in (*.*) do (
CALL:PROCESS "%%f")
echo/Finished. %count% files copied
popd&exit/b
:PROCESS file
cls
if "%~x1" == "" (exit/b)
echo/Full path: %~f1&echo/Disk: %~d1
echo/Name: %~n1&echo/Extension: %~x1
echo/Attributes: %~a1&echo/Time %~t1
echo/Size: %~z1 bytes&echo/
set /p "choice=Do you want to copy %1? [Y/N] "
if /i "%choice%" == "y" (
Goto Y)
echo/Skipped&timeout 2 1>nul&exit/b
:Y
copy "%~1" "%destination%\%~1" >nul || GOTO FAIL
attrib +R "%destination%\%~1" >nul || GOTO FAIL
:NEXT
set /a count=count+1
echo/Precess sucefull!&timeout 2 1>nul&exit/b
:FAIL
echo/Failed to copy %~1
pause>nul
exit/b
Well if you want to copy all files of all subfolders, you can use for /r
save it as copysec.bat.

Related

error loading text as variable from file and check it

this is some code it suppose to check for adb.exe in a location then if it exist will proceed to code if not will ask user to input location manually and check again if location is right will save the input in file "dontremoveoredit" to use it next time user open the bat file
the problem is if the file is empty or contain path it will crash the bat and close
:: #echo off
If exist "C:\Program Files\ui\adb.exe" (
goto yes
) else (
goto adbexist
)
:yes
set PATH=%PATH%;C:\Program Files\ui"
:: #cd/d "%~dp0"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
Echo.
Echo.
Echo.
echo Yes Was Executed
Pause>nul
Exit
:no
msg * "Couldn't find The adb.exe in Default Bath"
ping localhost -n 3 >Nul
goto noadbtext
:adbexist
If exist dontremoveoredit (
goto adbexist1
) else (
goto no
)
:adbexist1
set /p var=<dontremoveoredit
if [%var%] == [] (
goto noadbtext
) else (
goto lala
)
:lala
If exist %var%\adb.exe (
set PATH=%PATH%;%var%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
goto dns
) else (
msg * "Make Sure You Entered The Right Path To UI"
goto noadbtext
)
:noadbtext
break>"dontremoveoredit"
set /p EmulatorUIBath=Enter Your Emulator's UI folder Path :
If exist "%EmulatorUIBath%\adb.exe" (
#echo %EmulatorUIBath%>> "dontremoveoredit"
goto adbexist2
) else (
msg * "Make Sure You Entered The Right Path To UI"
goto noadbtext
)
:adbexist2
set PATH=%PATH%;%EmulatorUIBath%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
echo adb Exist2
Pause>nul
exit
Here you are my laptop is about to die but this should do the needful nicely. :)
Slightly Updated Version I mentioned I woudl post.
#(
SETLOCAL EnableDelayedExpansion
ECHO OFF
SET "_ConfigFilePath=%~0dpn_DO_NOT_REMOVE.dat"
SET "_ADBPath="
SET "_eLvl=0"
SET "ECHO_RW=Call :Echo_Color CF "
SET "ECHO_GB=Call :Echo_Color 20 "
SET "ECHO_AB=Call :Echo_Color B0 "
SET "ECHO_LY=Call :Echo_Color 1E "
SET "ECHO_YL=Call :Echo_Color E1 "
SET "ECHO_YR=Call :Echo_Color EC "
SET "ECHO_RY=Call :Echo_Color CE "
SET "ECHO_BY=Call :Echo_Color 1E "
)
CALL :Main
(
ECHO. Script Completed With ErrorLevel of %_eLvl%
ENDLOCAL
Exit /B %_eLvl%
)
:Echo_Color
ECHO.>"%~2"
FINDStr /A:%~1 /I ".*" "%~2" NUL
DEL /F /Q "%~2" >nul 2>nul
GOTO :EOF
:Main
REM Checing for ADB
CALL :CheckADB
IF /I %_eLvl% NEQ 0 (
ECHO. Error Encountered, Exiting!
) ELSE (
CALL :RunADB
)
PAUSE
GOTO :EOF
:CheckADB
REM Check For ADB Installed in Program Directories
FOR /F "Tokens=1* Delims==" %%A IN ('
SET ProgramFiles
') DO (
FOR /F "tokens=*" %%a IN ('
WHERE /R "%%A" /F "adb.exe" 2^>nul
') DO (
ECHO Found ADB here: "%%~a"
SET "_ADBPath=%%~a"
)
)
REM ECHO.Finished Where Loop
IF NOT DEFINED _ADBPath (
ECHO. ADB Install not Found, checking Config file.
IF EXIST "!_ConfigFilePath!" (
ECHO. Config File Exists checking contents..
FOR /F "Tokens=*" %%A IN ('
TYPE "!_ConfigFilePath!"
') DO (
ECHO. Saved ADB Path Found: "%%~A" in Config file.
IF EXIST "%%~A" (
SET "_ADBPath=%%~A"
) ELSE (
ECHO. Saved ADB Path "%%~A" Does Not Exist! Deleteing Config File.
CALL :Get_ADB_Path_From_User
)
)
) ELSE (
CALL :Get_ADB_Path_From_User
)
)
GOTO :EOF
:Get_ADB_Path_From_User
CLS
REM COLOR CF
ECHO.
COLOR 1E
FOR %%A IN (
"==========================================================="
"= ADB was not detected on your system ="
"==========================================================="
) DO (
REM ECHO.>"%%~A"
REM FINDStr /A:CF /I ".*" "%%~A" NUL
REM DEL /F /Q "%%~A" >nul 2>nul
%ECHO_RY% "%%~A"
)
ECHO.
ECHO.
%ECHO_AB% "Please enter a custom path to ADB below."
ECHO.
ECHO.
SET /P "_TmpADBPath=What Is the Path to your ADB Install? "
CLS
IF NOT EXIST "%_TmpADBPath%" (
%ECHO_RW% "==========================================================="
%ECHO_RW% "== =="
%ECHO_RW% "== Unable to Verify that The path Provided Exists =="
%ECHO_RW% "== =="
%ECHO_RW% "==========================================================="
SET "_eLvl=1"
GOTO :EOF
) ELSE (
COLOR 2F
SET "_ADBPath=%_TmpADBPath%"
ECHO.===========================================================
ECHO. Path Exists:
ECHO.
ECHO. "%_TmpADBPath%"
ECHO.
ECHO. ===========================================================
ECHO.
ECHO.
CHOICE /M "Save this Path for future Use?"
IF /I "%ERRORLEVEL%" EQU "0" (
ECHO.%_TmpADBPath%>"%_ConfigFilePath%"
ECHO.
ECHO. Saved to: "%_ConfigFilePath%"
) ELSE (
ECHO.
ECHO. Okay, Will only use it temporarily.
)
COLOR
)
GOTO :EOF
:RunADB
"%_ADBPath%" kill-server
"%_ADBPath%" devices
"%_ADBPath%" connect localhost:5037
Echo.
Echo.
Echo.
Echo.Yes Was Executed
Pause>nul
Exit

Moving text files one by one using batch-file with a timeout

hi guys i'm a trying to move text files one by one with a timeout of 8 seconds from one folder to another using a batch script. i have this script so far;
move /-y "D:\example\original\*2007*.txt" "D:\example\New folder\"
what should i add so that it doesn't move the files at once?
As #npocmaka recommend to you the user confirmation for example like this batch script : Movies wrapper script
#ECHO OFF
SETLOCAL
SET "ROOT=%~dp0"
SET "FORCE=0"
IF /I "%~1"=="/f" (
SET "FORCE=1"
SHIFT
)
IF "%~1"=="" GOTO :END_PARSE
SET "ROOT=%ROOT%%~1\"
:END_PARSE
ECHO -----------------------------------------------
ECHO WRAPPER - "%ROOT%"
ECHO -----------------------------------------------
SET "COUNT_SUCC=0"
SET "COUNT_FAIL=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
FOR %%f IN ("%ROOT%\*.*") DO (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
IF !FORCE! NEQ 1 (
rem echo force=!Force!
SET /P "INPUT=Do you want to wrap the file "%%~nxf" ? (Y/[N])"
IF /I "!INPUT!"=="Y" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\">NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\">NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
) Else (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
)
)
)
ECHO.
SET /a "COUNT_TOT=COUNT_SUCC+COUNT_FAIL"
ECHO Total of %COUNT_TOT% files(s) : %COUNT_SUCC% file(s) wrapped and %COUNT_FAIL% file(s) failed.
ECHO.
PAUSE
use a simple for loop to process each file individually:
for %%a in ("D:\example\original\*2007*.txt") do (
move /-y "%%~fa" "D:\example\New folder\"
timeout /t 8
)
For more information, see for /?

Directory traversal and file selection with .bat

I am trying to write a .bat file that allows me to traverse through directories (up or down) and let me select a file from the current directory, passing that filename out at the end of the routine. Ideally, it would handle if it is at the root of a drive (i.e. C:) or that there are no more sub directories.
(If there are more elegant ways of doing what I am asking, please feel free to suggest them!)
#echo off
setlocal enabledelayedexpansion
set FVAR=
:start
::-------------------------------------------------------
:: LIST - Lists all files in the current folder
::-------------------------------------------------------
:LIST
echo.
if exist . echo ^<DIR^> .
if exist .. echo ^<DIR^> ..
for /f "tokens=* delims=" %%a in ('dir /b /ad') do (
echo ^<DIR^> %%a
)
for /f "tokens=* delims=" %%a in ('dir /b /a-d') do (
echo %%a
)
::-------------------------------------------------------
:: INPUT - Requests filename as input from user
::-------------------------------------------------------
:INPUT
echo.
set /p FVAR="Choose your file [HINT: hit <TAB> to cycle the current folder contents]: "
echo.
echo %FVAR%
if not defined FVAR (goto TRYAGAIN)
set FVARFLAG1=0
set FVARFLAG2=0
set FVARFLAG=%FVARFLAG1%%FVARFLAG2%
echo %FVARFLAG%
if exist %FVAR%\ set "%FVARFLAG1%"=="1"
if exist %FVAR% set "%FVARFLAG2%"=="1"
set FVARFLAG=%FVARFLAG1%%FVARFLAG2%
echo %FVARFLAG%
if "%FVARFLAG%"=="00" goto TRYAGAIN
if "%FVARFLAG%"=="01" goto FILE
if "%FVARFLAG%"=="10" goto DIR
if "%FVARFLAG%"=="11" goto TRYAGAIN
goto TRYAGAIN
:DIR
if exist %FVAR%\ (
echo Successfully set dir name!
goto END
)
goto TRYAGAIN
:FILE
if exist %FVAR% (
echo Successfully set file name!
goto END
)
goto TRYAGAIN
rem if /i "%option:"=%"=="Y" goto YES //This line left in for future use
rem if /i "%option:"=%"=="N" goto NO //This line left in for future use
goto END
::-------------------------------------------------------
:: TRYAGAIN - Returns user to input menu on invalid entry
::-------------------------------------------------------
:TRYAGAIN
echo ------------------------------
echo Invalid selection...try again
echo ------------------------------
goto INPUT
:END
goto :EOF
I like this application! The use of arrays allows you to write simpler and more powerful code. This is my version:
#echo off
setlocal EnableDelayedExpansion
rem Select a file browsing a directory tree
rem Antonio Perez Ayala
set pageSize=30
rem Load current directory contents
:ProcessThisDir
for /F "delims==" %%a in ('set name[ 2^>NUL') do set "%%a="
set numNames=0
for /D %%a in (*) do (
set /A numNames+=1
set "name[!numNames!]=<DIR> %%a"
)
for %%a in (*.*) do (
set /A numNames+=1
set "name[!numNames!]= %%a"
)
rem Show directory contents, one page at a time
set start=1
:ShowPage
if %start% equ 1 (
set "less="
) else (
set "less=-=Previous page, "
)
set /A end=start+pageSize-1
if %end% gtr %numNames% (
set end=%numNames%
set "more="
) else (
set "more=+=Next page, "
)
cls
echo Directory: %CD%
echo/
for /L %%i in (%start%,1,%end%) do echo %%i- !name[%%i]!
echo/
:GetOption
set "option="
set /P "option=Enter desired item (%less%%more%Nothing=..): "
if not defined option (
cd ..
goto ProcessThisDir
) else if "%option%" equ "-" (
set /A start-=pageSize
if !start! lss 1 set start=1
goto ShowPage
) else if "%option%" equ "+" (
if defined more set /A start+=pageSize
goto ShowPage
) else if not defined name[%option%] (
goto GetOption
) else if "!name[%option%]:~0,5!" equ "<DIR>" (
cd "!name[%option%]:~8!"
goto ProcessThisDir
)
rem Return selected file
cls
for %%a in ("!name[%option%]:~8!") do set "result=%%~Fa"
echo Result="%result%"

How to write a batch file to check for specifed folders in a directory

How can I write a batch file to check for specified subfolders in a directory and then generate a log file showing the results of the findings (found it or not found). I am new to scripting and below is what I have so far:
#echo off
ECHO
SET /P QUESTION="Perform file check (Y/N)?"
if QUESTION == y goto :START_SCRIPT
:START_SCRIPT
if exist "C:\Folder\ABC" (echo found it) else echo not found;
if exist "C:\Folder\DEF" (echo found it) else echo not found;
if exist "C:\Folder\GHI" (echo found it) else echo not found;
test.bat >> out.txt
One more
#echo off
set "folder=c:\folder"
( for %%d in ("ABC" "DEF" "GHI") do if exist "%folder%\%%~d" (
echo "%%~d" found
) else (
echo "%%~d" not found
)
) >> file.log
Very simple;
#echo off
choice /m "Perform Check?"
if errorlevel 2 Exit
:loop
set /p query="Folder Name: "
if "%query%"=="" goto :end
if exist "C:\Folder\%query%" (Echo %query% Exists&Echo %query% Found >> out.txt
) Else (
Echo %query% Doesn't Exist&Echo %query% Not Found >> out.txt
)
goto :loop
:end
Echo End of Batch File&sleep 5
Exit
And that should do what you want (note it requires user input) which if is not the case, can use a log file redirected as input.
Mona
Here is how I'd do it:
#echo off
setlocal
cd /d %~dp0
call :FolderExists abc && Echo Yes || Echo No
exit /b
:FolderExists folder
for /f %%a in ('dir /b /ad') do (
if /i %%a EQU %1 exit /b 0)
exit /b 1

How can I set a batch file to find a file and show its emplacement?

I need this for a basic program. What i can do so far is track if a predefined file exists, but not much more.
I'm trying to do this:
Select a file
See if it exists
Find the path to it
save the path under a variable, or something like that
Show the path to the user
(Execute the file, optional)
Is there a way to do it, and what is the script?
Please tell me if i am not clear.
Thanks in advance
#echo off
setLocal
set /p file=choose a file:
set /p option=do you want to execute the file?[y/n]
if "%option%" equ "y" set /p directory=directory where you want to execute the file:
if "%option%" equ "Y" set /p directory=directory where you want to execute the file:
if not exist "%directory%\" echo directory does not exist %% exit /b 3
if not exist %file% echo file does not exists && exit /b 1
for %%F in (%file%) do (
set file_atr=%%F
set full_path=%%~fF
)
if "%file_atr::~0,1%" equ "d" echo this is a directory && exit /b 2
echo the full path is : %full_path%
if "%option%" equ "y" (
pushd %directory%
call %full_path%
popd
)
if "%option%" equ "Y" (
pushd %directory%
call %full_path%
popd
)
endLocal

Resources