Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: FILE CONTAINS - see if a file contains some text[Next]: FOR Loops - for x = 1 to 10
\->Batch Files->FILE NAME Manipulation - returns a files fullname, shortname, extension, path etc

FILE NAME Manipulation [returns a files fullname, shortname, extension, path etc].cmd

See the "parameter replacement" section and the "FOR" command for more details about the file related command extensions.

Some of the manipulations require access to the file for this reason the second loop in the example below will be slower.

The first loop uses the batch files short name (no path) and the second loop uses the full UNC name to that same batch file and will work if the "C$" share exists and can be accessed with your userid.

[anchor]

The Code for: "FILE NAME Manipulation [returns a files fullname, shortname, extension, path etc].cmd"

This is the example, a shortcut was installed to this code :

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].FILE NAME Manipulation [returns a files fullname, shortname, extension, path etc].cmd.pvcs   1.0   29 Jun 2014 12:51:20   USER "$
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocal


::--- Can directly manipulate "%0" - "%9" -----------------------------------
set    BatchFile=%~f0
set BatchFileDir=%~dp0

::--- Display the results ---------------------------------------------------
echo BatchFile     = %BatchFile%
echo BatchFileDir\ = %BatchFileDir%


::--- Start of Loop To Handle one file (2 loops) ----------------------------
set UncFile=\\%COMPUTERNAME%\%BatchFile::=$%
set TheFile=%~0
:ANOTHER_FILE
    ::--- Display the filename ----------------------------------------------
    echo.&echo.
    echo %TheFile%
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    ::--- For anothing else use the "FOR" command -------------------------------
    for %%X in ("%TheFile%") do set   FullName=%%~fX
    for %%X in ("%TheFile%") do set Full83Name=%%~fsX
    for %%X in ("%TheFile%") do set  FileDrive=%%~dX
    for %%X in ("%TheFile%") do set   FilePath=%%~pX
    for %%X in ("%TheFile%") do set   FileExtn=%%~xX
    for %%X in ("%TheFile%") do set   FileAttr=%%~aX
    for %%X in ("%TheFile%") do set   FileTime=%%~tX
    for %%X in ("%TheFile%") do set   FileSize=%%~zX
    for %%X in ("%TheFile%") do set FileNoPath=%%~nX%%~xX
    for %%X in ("%TheFile%") do set   BaseName=%%~nX%
    for %%X in ("%TheFile%") do set    DirLike=%%~ftzaX

    ::--- Display the results ---------------------------------------------------
    echo FullName   = %FullName%
    echo Full83Name = %Full83Name%
    echo FileDrive  = %FileDrive%
    echo FilePath\  = %FilePath%
    echo FileExtn   = %FileExtn%
    echo FileAttr   = %FileAttr%
    echo FileTime   = %FileTime%
    echo FileSize   = %FileSize%
    echo ShortName  = %FileNoPath%
    echo BaseName   = %BaseName%
    echo DIR Like   = %DirLike%

::--- Want to do 2 times, one "normal" file and the other UNC ---------------
if not "%TheFile%" == "%UncFile%" set TheFile=%UncFile%& goto ANOTHER_FILE

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: FILE CONTAINS - see if a file contains some text[Next]: FOR Loops - for x = 1 to 10


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.