?
Solved

Create new file automatically according to existing file

Posted on 2011-10-30
14
Medium Priority
?
236 Views
Last Modified: 2012-08-13
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,
0
Comment
Question by:softbless
  • 8
  • 4
  • 2
14 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37055160
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37055205
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
 

Author Comment

by:softbless
ID: 37055243
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 43

Expert Comment

by:Steve Knight
ID: 37055254
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37055259
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
 

Author Comment

by:softbless
ID: 37055320
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37055467
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37067504
@softbless

Still having trouble, or does this work for you?

~bp
0
 

Author Comment

by:softbless
ID: 37067648
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37068708
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37069075
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37069292
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 37069303
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
 

Author Closing Comment

by:softbless
ID: 37143104
Thank you very much...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Loops Section Overview
Screencast - Getting to Know the Pipeline

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question