Why is my batch script hiding processed files/folders? - batch-file

Can anyone here give me a hint, why my batch script marks successfully processed folders as system, hidden, non-archive? Furthermore I cannot even remove the "hidden" attribute via Explorer (probably because of the systemfolder attribute).
The script is meant to process one folder (passed to it as a parameter), looking for raw-photo files (.nef files in my case) that are marked read-only. For every read-only photo the script copies a specified file to the processed folder and renames that copy according to the photo filename.
The folder attribute mess is caused by robocopy. (Without that command there is no problem.) But it doesn't have to touch the folder at all. It only copies one file to that folder. The error only occurs, if at least one file in the folder was marked read-only and gets a sidecar file.
I already tried to move the script from system drive to desktop and start it from there. It made no difference.
(To avoid confusion: I am on a non-English Windows 10, so I used !var! instead of %var%. Hell it took some time to find that trick...)
echo off
setlocal ENABLEDELAYEDEXPANSION
chcp 65001
IF "%~1" == "" (
GOTO myWarning
) ELSE (
IF EXIST "%~1" (
GOTO myFuction
) ELSE (
GOTO myWarning
)
)
GOTO myFuction
:myWarning
echo Ordner-Pfad muss angegeben werden!
pause
GOTO:eof
:myFuction
echo Bearbeite %1
cd "%1"
for /r %%f in (*.nef) do (
set fileattr=%%~af
set readonlyattr=!fileattr:~1,1!
:: check if current file is read-only
IF !readonlyattr!==r (
:: create XMP-Sidecar file for read-only photos
echo %%f
robocopy "C:" "%1" "metadata-2stars.xmp"
rename "metadata-2stars.xmp" "%%~nf.xmp"
)
)
GOTO:eof

Sorry, after I narrowed the problem down to robocopy I found the solution. It seems to be a known bug in robocopy, e.g. described here:
https://blog.coeo.com/how-to-prevent-robocopy-from-hiding-your-files-and-how-to-fix-it-when-it-does
The solution/hotfix is simply to tell robocopy not to mark the destination as system hidden by adding /A-:SH at the end of the command. So with robocopy "C:" "%1" "metadata-2stars.xmp" /A-:SH everything works as expected.

Related

How do I set an output folder for my .wem converter?

I'm trying to make that when I run the script and there's a .wem file in my "input" folder, it will directly convert it into my "output" folder. But I can't seem to figure this out.
#Echo off
Echo -------------------------------------------------------------------------------------
For %%f in ("%~dp0input\*.wem") do "./ww2ogg024/ww2ogg.exe" --pcb "./ww2ogg024/packed_codebooks_aoTuV_603.bin" "%%f"
For %%f in ("%~dp0outnput\*.ogg") do revorb.exe "%%f"
Echo -------------------------------------------------------------------------------------
pause
Does anyone know what am I doing wrong? I'm kind of a starter with this.
I have no knowledge of your executables, however, a quick search revealed their command line options:
ww2ogg input.wav [-o output.ogg] [--inline-codebooks] [--full-setup]
[--pcb packed_codebooks.bin]
 
revorb <input.ogg> [output.ogg]
From that information, I'd suggest that you try this sort of methodology, (Remarks included as explanation):
#Echo Off
Rem Define the location for ww2ogg.exe.
Set "WemToOggPath=C:\SomeLocation\ww2ogg024"
Rem Define the location for revorb.exe.
Set "MyRevorbPath=C:\SomeLocation"
Rem Exit if the required executables and input files are not available.
If Not Exist "%WemToOggPath%\ww2ogg.exe" Exit /B 1
If Not Exist "%MyRevorbPath%\revorb.exe" Exit /B 1
If Not Exist "%~dp0input\*.wem" Exit /B 1
Rem Create output directory if it does not already exist along side this script.
If Not Exist "%~dp0output\" MD "%~dp0output"
Echo -------------------------------------------------------------------------------------
Rem Loop through each .wem file located inside the a directory named input along side this script.
For %%I In ("%~dp0input\*.wem") Do (
Rem Run ww2ogg.exe against each .wem file outputting them to the same directory but with an .ogg extension.
"%WemToOggPath%\ww2ogg.exe" "%%I" -o "%%~dpnI.ogg" --pcb "%WemToOggPath%\packed_codebooks_aoTuV_603.bin"
Rem If the last ww2ogg.exe process was successful then.
If Not ErrorLevel 1 If Exist "%%~dpnI.ogg" (
Rem If there is still a .wem file delete it.
If Exist "%%I" Del "%%I"
Rem Run revorb.exe against the .ogg file outputting it to the output directory along side this script.
"%MyRevorbPath%\revorb.exe" "%%~dpnI.ogg" "%~dp0output\%%~nI.ogg"
Rem If the last revorb.exe process was successful and there is still a matching .ogg file inside the input directory delete it.
If Not ErrorLevel 1 If Exist "%%~dpnI.ogg" If Exist "%~dp0output\%%~nI.ogg" Del "%%~dpnI.ogg"
)
)
Echo -------------------------------------------------------------------------------------
Pause
As I don't know those utilities, I have tried to assume nothing, so if your conversion processes are not leaving the unconverted files behind, the script could probably be made smaller.
Please read through the Remarks to understand exactly what it does, before you run it, and most importantly, ensure that you modify C:\SomeLocation on lines 3 and 5 to those which hold your two executables. (Please do not leave trailing path separators on those directories). I would then suggest that you try the script from within a test input directory, and against some copied .wem files, before attempting it in your production environment.

Why does my batch file create an additional copy in current folder?

I have a folder xyz. It has a batch file which copies files from source directory abc to destination directory def. I am using copy with options /v /y.
Copy works absolutely fine. But I notice a strange or weird issue that additionally a copy of all the files copied from source to destination are present in folder xyz.
I started observing this issue after a system restart and not sure if its a one time issue. But I would like to know if someone has run into this issue before and what is the possible fix?
Here is the code:
#if not defined ECHO_ON echo off
SETLOCAL EnableDelayedExpansion
set arg1=%1
set arg2=%2
copy /v /y !arg1! !arg2!
call :getPath !arg1!
ren !arg2!\!_NAME_EXT! !_NAME!.svg
:getPath
set _NAME=%~n1
set _NAME_EXT=%~nx1
set _LOC=%~dp1
goto:eof
endlocal
Please note I am using copy and robocopy command (for some other copying operation) in same .bat file.
Is this something to be worried about?
(As I wrote things worked fine until restart.)
Your double Copy is because a batch script works line by line until it reaches an end of file marker or an exit instruction. A Call command returns back to the point just after the Call instruction. When it returns, there is no exit instruction or end of file marker until the bottom of your script, so the :getPath label is executed again.
There appears to have been absolutely no reason for EnableDelayedExpansion in your script, for Setting any variables or for a Call command. I have therefore simplified it as such:
#Echo Off
If "%~2"=="" Exit /B
If Not Exist "%~2\" MD "%~2" 2>Nul || Exit /B
If Exist "%~1" Copy /V /Y "%~1" "%~2\%~n1.svg"
I hope it helps you out.

How locate a file's location and CD to the directory?

I'm making a batch that edits a document, but in order to edit, it need's to CD it its location. The problem I'm having is that in order to make it portable, I need the command to be able to locate the file's location.
I've tried:
CD C:\Users\%username%\AppData\Roaming\skype\John
CD C:\Users\%username%\AppData\Roaming\skype\%foldername%\config.xml
Any way to get to the location with the config.xml?
You can try with:
FOR /D %%G IN ("%APPDATA%\skype\*") DO IF EXIST "%%~fG\config.xml" (
set correctDir=%%G
goto :foundFile
)
echo File config.xml not found
goto :eof
:foundFile
cd "%correctDir%"
FOR /D iterates over all directories using the %%G variable. %%~fG expands to the full path to the directorie in %%G.
IF EXIST checks if a file exists.
goto :eof exits the script
EDIT: As #Compo pointed out: for portability reasons it is better to use the OS built-in environment variable %APPDATA% instead of C:\Users\%username%\AppData\Roaming.

batch file : overwrite one file to several existing files

I am trying to make simple security program for my company.
We usually make a lot of doc or ppt(x) files and
for some reason, we need to make them disable as soon as possible.
So we usually deleted the all of files but It took so long.
So I thought I can do that by overwritting the files.
If I have a empty doc or ppt files then overwite all of doc, ppt files in working drive each, then it will be faster and much safer than just deleting.
So I tried to use xcopy
Assuming empty.doc is just empty doc file and
xcopy /s /y c:\users\mycom\empty.doc c:\*.doc
But it said cannot perform cyclic copy
I need you guys help
and I am glad to hear suggestion.
Thanks.
This is an old, old batch file that I employed for such an endeavour:
:: dispose.bat
#echo off
:: Check parameter(s), stripping quotes
set yp1=%1
set ydf=
:insistp1
if defined yp1 set yp1=%yp1:"=%
if not defined yp1 for %%i in (echo goto) do %%i error - filename required
if not exist "%yp1%" for %%i in (echo goto) do %%i error - file not found
for %%i in ("%yp1%") do (set yfr=%%i&call :zapit %%~di) 2>nul
:error
:: Clean up variables used
if defined ydf echo Warning! dispose failed!!
for %%i in (yp1 yfr yrn ydf) do set %%i=
goto :eof
:zapit
set yfr=%yfr:"=%
IF /i %1 == u: DEL "%yfr%" &goto :eof
if not exist %1\delete\. md %1\delete
(set yrn=)
:rndloop
set yrn=%yrn%%random%
if exist %1\delete\%yrn% goto rndloop
if not exist "%yfr%" set ydf=Y&goto :eof
move "%yfr%" %1\delete\%yrn%>nul
goto :eof
:: dispose.bat ends
Noting that u: is a RAMDRIVE on my system, hence mere deletion is all that is required.
The purpose is not to actually delete the files, but to move them to a directory named ?:\delete and provide them with a random name in that directory.
Since the file is simply MOVEd it is quite fast, which addresses your time consideration.
An issue for me is the idea of copying a file over all of the files you target. If the file that you copy is shorter than the other files, some data wilstill be available to be recovered. Regardless, it will alwats be slower than simply deleting the files (which you say you are currently doing.)
This scheme simply accumulates the files-to-be-deleted in a known directory on the same drive (so they will simply be moved, not copied.)
Once they are in your \delete directory, you can let a utility like ccleaner or recuva loose on that single directory in the background and it will overwrite the files a specified number of times.
Here's a simpler method. Be careful.
At the command line:
for /r c:\ %A in (*.doc? *.ppt?) do echo. > %A
In a batch file:
for /r c:\ %%A in (*.doc? *.ppt?) do echo. > %%A
EDIT:
To replace with a file, see the example below. Replace the example's d:\path\file.ext with your intended file. Note that the previous option will work much faster with a similar result.
At the command line:
for /r c:\ %A in (*.doc? *.ppt?) do copy d:\path\file.ext > %A
In a batch file:
for /r c:\ %%A in (*.doc? *.ppt?) do copy d:\path\file.ext > %%A
Either way, as noted in Magoo's answer, larger files will still have recoverable data on the drive. You stated in a comment:
But if I overwrite the original files, then they cannot guess what it
was unless they got bak files
This isn't accurate. Forensic tools can retrieve the partial data that wasn't overwritten with new content.

Batch file copying issue

I'm having trouble making a batch file that copies files. Sometimes it says that the directories exist, other times it says that it can not perform a cyclic copy.
#ECHO off
ECHO Please use quotes with directories
ECHO.
IF NOT EXIST Pictures (MD Pictures) > NUL
:start
SET /P From=Copy from:
IF NOT EXIST %From% (ECHO No such directory
ECHO.
goto start)
XCOPY /s %From% Pictures
pause
This is another way to do it: xcopy creates the folder by itself and the slash at the end of the target path stops it from prompting you.
The slash at the if exist "folder\" is used to detect a folder on a local drive, and not a file.
Stephan's answer tells you how to avoid a cyclic copy error.
#ECHO off
ECHO Please use quotes with directories
ECHO.
:start
SET /P From=Copy from:
IF exist "%From%\" (
XCOPY /s %From% "Pictures\"
) else (
ECHO No such directory
ECHO.
goto start
)
pause
if you try to create a directory, that already exists, md tells you. You can suppress it with
md pictures 2>nul
The parameter /s with xcopy tells the computer to copy subdirectories and their content too. So you would make a copy of a copy of a copy of a copy.... (called cyclic copy). To avoid this, don't use /s or make sure, the destination directory is outside the directory-tree, you want to copy.

Resources