Path placeholders can be used in the replacement text on the Replace and Sequence pages, as well as in the text to be collected on the Collect page. For this to work, the option “Expand match placeholders and path placeholders” must have been enabled in the action & results preferences.
The placeholders allow you to use the full path or parts of the path to the file that PowerGREP is searching through in replacements and collections. When processing a file, PowerGREP will set %FILE% to the full path to the file, and compute the other placeholders from that.
If the file is inside an archive, PowerGREP will treat the path to the archive as the folder containing the file. E.g. when searching through a file zipped.txt in an archive c:\data\archive.zip, then %FILE% is set to c:\data\archive.zip\zipped.txt. If the archive contains a folder structure, and PowerGREP is searching through zipfolder\zipped.txt in the same archive, then %FILE% is set to c:\data\archive.zip\zipfolder\zipped.txt.
Placeholder | Meaning | Example |
%FILE% | The entire path plus filename to the file | C:\data\files\web\log\foo.bar.txt |
%FILENAME% | The file name without path | foo.bar.txt |
The file name without the extension | foo.bar | |
The file name cut off at the first dot | foo | |
The file name without the .gz, .bz2, or .xz extension | foo.bar | |
%FILEEXT% | The extension of the file name without the dot | txt |
%FILELONGEXT% | Everything in the file name after the first dot | bar.txt |
%PATH% | The full path without trailing delimiter to the file | C:\data\files\web\log |
%DRIVE% | The drive the file is on. | C: for DOS paths \\server for UNC paths blank for UNIX paths |
%FOLDER% | The full path without the drive and without leading or trailing delimiters | data\files\web\log |
%FOLDER1% | First folder in the path | data |
%FOLDER2% | Second folder in the path | files |
(...etc...) | ||
%FOLDER99% | 99th folder in the path. | In this example, this tag will be replaced with nothingness, because there are less than 99 folders. |
%FOLDER<1% | Last folder in the path | log |
%FOLDER<2% | Second folder from the end in the path | web |
(...etc...) | ||
%FOLDER<99% | 99th folder from the end in the path. | In this example, this tag will be replaced with nothingness. |
%PATH1% | First folder in the path | data |
%PATH2% | First two folders in the path | data\files |
(...etc...) | ||
%PATH99% | First 99 folders in the path | data\files\web\log |
%PATH<1% | Last folder in the path | log |
%PATH<2% | Last two folders in the path | web\log |
(...etc...) | ||
%PATH<99% | Last 99 folders in the path | data\files\web\log |
%PATH-1% | Path without the drive or the first folder | files\web\log |
%PATH-2% | Path without the drive or the first two folders | web\log |
(...etc...) | ||
%PATH-99% | Path without the drive or the first 99 folders. | In this example, this tag will be replaced with nothingness. |
%PATH<-1% | Path without the drive or the last folder | data\files\web |
%PATH<-2% | Path without the drive or the last two folders | data\files |
(...etc...) | ||
%PATH<-99% | Path without the drive or the last 99 folders. | In this example, this tag will be replaced with nothingness. |
Examples: Process files in a batch file or script, Compile indices of files and Generate a PHP navigation bar
You can string several path placeholders together to form a complete path. If you have a file c:\data\test\file.txt then d:\%FOLDER2%\%FILENAME% will be substituted with d:\test\file.txt However, if the original file is c:\more\file.txt then the same path will be replaced with d:\\file.txt because %FOLDER2% is empty. The result is an invalid path.
The solution is to use combined path placeholders, like this: d:\%FOLDER2\FILENAME%. The first example will be substituted with c:\test\file.txt just the same, and the second will be substituted with d:\file.txt, a valid path. You can combine any number of path placeholders into a single path placeholder, separating them either with backslashes (\) or forward slashes (/). Place the entire combined placeholder between two percentage signs.
A slash between two placeholders inside the combined placeholder is only added if there is actually something to separate inside the placeholder. Slashes between two placeholders will never cause a slash to be put at the start or the end of the entire resulting path. In the above example, the backslash inside the placeholder is only included in the final path if %FOLDER2% is not empty.
A slash just after the first percentage sign makes sure that the resulting path starts with a slash. If the entire resulting path is empty, or if it already starts with a slash, then the slash is not added.
A slash just before the final percentage sign makes sure that the resulting path ends with a slash. If the entire resulting path is empty, or if it already starts with a slash, then the slash is not added.
Mixing backslashes and forward slashes is not permitted. Using a forward slash inside a combined placeholder, will convert all backslashes in the resulting path to forward slashes. This is useful when creating URLs based on file names, as URLs use forward slashes, but Windows file names use backslashes.
Example: If the original path is c:\data\files\web\log\foo.bar.txt