Create new file automatically according to existing file

Hi experts,

In this case, in a folder there are alot of files and subfolders.
For example for folder 'Data':

Data
-Book.xls
-Sample.doc
-Hello.pdf
-Images
  -IMG1.jpg
  -IMG2.jpg

So I want a batch script file to create new file according to existing file, so that:
Data
-Book.xls
-Book.xls.metadata.properties.xml
-Sample.doc
-Sample.doc.metadata.properties.xml
-Hello.pdf
-Hello.pdf.metadata.properties.xml
-Images
  -IMG1.jpg
  -IMG1.jpg.metadata.properties.xml
  -IMG2.jpg
  -IMG2.jpg.metadata.properties.xml

So that script will create file with filename plus metadata.properties.xml, where all files with filename metadata.properties.xml still empty. It just a empy file, only create new file with filename plus metadata.properties.xml from existing file.

Hope anyone help me and sorry for my english before :)

Regards,
softblessAsked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
Looks like it posted that time.... sadly I didn't save it last time so had to type that again :-(

Oh the counts will count any errors showing in the fail file too:

So remove

 2>>%fail%

Steve
0
 
Steve KnightIT ConsultancyCommented:
Ok we can do that with a quick batch file.  As it stands you can either run it without any parameters and it will make the changes for the dir shown in the cd command in the batch file or you can run it from cmd.exe prompt with:

yourbatch.cmd "c:\somepath"

or you can drag a folder onto the batch file in explorer.

it works by doing a dir listing, excluding any lines with metadata.properties.xml in them so it doesnt add it twice and then for each file creating a new 0 byte one with a copy command.

if you want it to also do all subdirs of the dir then add /s after the /ad  and /b on the dir command.

this is untested at the mo. As on mobile.  Please try on test dir first.

Hth

Steve

@echo off
REM change into specific dir using cd command, or drag dir onto this batch file in explorer
if "%~1"=="" (
  cd /d C:\somedir
) ELSE (
  cd /d "%~1"
)
set CHECK=
set /p CHECK=Add files to this dir [Y/N] : %cd% :
if /i not "%CHECK%"=="Y" (
  echo Closing batch file.  No changes made.
  pause
  exit /b
)

for /f "delims= %%F in ('dir /b /a-d ^| find /v /i "metadata.properties.xml"') do (
  echo Adding file for %%~nxF
  copy nul "%%~fF.metadata.properties.xml"
)
echo All complete
pause
0
 
Steve KnightIT ConsultancyCommented:
Sorry one " missing there typed earlier.  Have tested it now.

Also have added check to see if the metadata XML file exists for a file and skips it if so therefore if you run over an existing directory it will only add missing ones, I presume that is what you would want?

Steve
@echo off
REM change into specific dir using cd command, or drag dir onto this batch file in explorer
if "%~1"=="" (
  cd /d C:\somedir
) ELSE (
  cd /d "%~1"
)
set CHECK=
set /p CHECK=Add files to this dir [Y/N] : %cd% : 
if /i not "%CHECK%"=="Y" (
  echo Closing batch file.  No changes made.
  pause
  exit /b
) 

for /f "delims=" %%F in ('dir /b /a-d ^| find /v /i "metadata.properties.xml"') do (

  if not exist "%%~fF.metadata.properties.xml" (
    echo Adding file for %%~nxF 
    copy nul "%%~fF.metadata.properties.xml" >NUL
  ) ELSE (
    echo Skipping already existing %%~nxF.metadata.properties.xml
  )

)
echo All complete
pause

Open in new window

0
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
softblessAuthor Commented:
Thanks dragon-it,

But for bat script, I don't confirmation.
So when I run (double click) the batch.cmd, it will create files as many as exsiting file with format filename file.pdf.metadata.properties.xml (this an example, not only for pdf) exclude file that have file name *.metadata.properties.xml.

Regards,
0
 
Steve KnightIT ConsultancyCommented:
Are you saying you just want it to do it without asking?

As it stands you can use it as shown above.  Hard code the path in the cd command or drag a directory to it in explorer or add to the command line.

If you don't want it to check whether this is the right dir have removed that check below:

Steve
@echo off
REM change into specific dir using cd command, or drag dir onto this batch file in explorer
if "%~1"=="" (
  cd /d C:\somedir
) ELSE (
  cd /d "%~1"
)
for /f "delims=" %%F in ('dir /b /a-d ^| find /v /i "metadata.properties.xml"') do (

  if not exist "%%~fF.metadata.properties.xml" (
    echo Adding file for %%~nxF 
    copy nul "%%~fF.metadata.properties.xml" >NUL
  ) ELSE (
    echo Skipping already existing %%~nxF.metadata.properties.xml
  )

)
echo All complete
pause

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
An yes it will go down all files in the dir asked, except those ...xml files, then checks if the xml file already exists, and if not creates it.

Currently runs for the directory shown only.  Add /s to the dir command as mentioned above to make it go down all subdirs under the dir too.



Steve
0
 
softblessAuthor Commented:
@dragon-it
I have tested it, but your program still need confirmation right? Actually I don't need confirmation for this case, so the program will create new file according existing files.
One more, you program didn;t work if my folder have subfolders and have subfolders and so on.

Can you complete it?
Thanks before
0
 
Steve KnightIT ConsultancyCommented:
It doesn't do any confirmation at all now unless you include the "pause" at the end which you can remove?

I already explained how to make it do subdirs too and that it doesn't at the moment.

It does seem a little rude when people ask questions about something you have already said several times?

Anyway as stated above already add "/s" to the dir command, i.e. as follows:

for /f "delims=" %%F in ('dir /b /a-d /s ^| find /v /i "metadata.properties.xml"') do (

I have copy/pasted that into the existing file for you....

Steve


@echo off
REM change into specific dir using cd command, or drag dir onto this batch file in explorer
if "%~1"=="" (
  cd /d C:\somedir
) ELSE (
  cd /d "%~1"
)
for /f "delims=" %%F in ('dir /b /a-d /s ^| find /v /i "metadata.properties.xml"') do (

  if not exist "%%~fF.metadata.properties.xml" (
    echo Adding file for %%~nxF 
    copy nul "%%~fF.metadata.properties.xml" >NUL
  ) ELSE (
    echo Skipping already existing %%~nxF.metadata.properties.xml
  )

)

Open in new window

0
 
Bill PrewCommented:
@softbless

Still having trouble, or does this work for you?

~bp
0
 
softblessAuthor Commented:
Hi billprew...

Yes, can you help me to make its log. The log consist of how many files inside the folder also in subfolders and how many files created?
So we can know how many files not successfully make ¿¿its metadata file?

Thanks,
0
 
Steve KnightIT ConsultancyCommented:
I have added here logging of whether a file is already there (skip.txt), created (good.txt), or can't create (fail.txt)

As to counts of files in folders can I ask that it is a new question please as we are going away from the original requirements and it's not as simple as it looks -- how about nested folders, 2,5, 10 levels deep etc?

Steve
0
 
Bill PrewCommented:
Sounds like Steve has those additional requirements covered, I'll leave him to assist unless he runs into a hurdle I can help with.

~bp
0
 
Steve KnightIT ConsultancyCommented:
Bizarre .... where did the code I posted before go :-(
@echo off  
set basedir=C:\logdir
set good="%basedir%\good.txt"
set fail="%basedir%\fail.txt"
set skip="%basedir%\skip.txt"

del %good% 2>NUL
del %fail% 2>NUL
del %skip% 2>NUL

REM change into specific dir using cd command, or drag dir onto this batch file in explorer  
if "%~1"=="" (  
  cd /d %basedir%  
) ELSE (  
  cd /d "%~1"  
)  
for /f "delims=" %%F in ('dir /b /a-d /s ^| find /v /i "metadata.properties.xml"') do (  
  
  if not exist "%%~fF.metadata.properties.xml" (  
    echo Adding file for %%~nxF   
    copy nul "%%~fF.metadata.properties.xml" >NUL 2>>%fail%
    if errorlevel 1 (
      echo %%~fF >>%fail%
    ) ELSE (
      echo %%~fF >>%good%
    )  
  ) ELSE (  
    echo Skipping already existing %%~nxF.metadata.properties.xml  
    echo %%~fF >>%skip%
  )  
  
) 

find "%basedir%\*.txt" /v "" /c > %basedir%\results.txt
notepad "%basedir%\results.txt"

Open in new window

0
 
softblessAuthor Commented:
Thank you very much...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.