{"id":131,"date":"2016-03-08T14:26:46","date_gmt":"2016-03-08T13:26:46","guid":{"rendered":"http:\/\/blogs.msdn.microsoft.com\/lavanack\/?p=131"},"modified":"2022-05-30T13:50:23","modified_gmt":"2022-05-30T11:50:23","slug":"ms-dos-current-dir","status":"publish","type":"post","link":"https:\/\/laurentvanacker.com\/index.php\/2016\/03\/08\/ms-dos-current-dir\/","title":{"rendered":"R\u00e9cup\u00e9rer le repertoire parent d&rsquo;un batch en MS-DOS \/ Retrieve the parent directory in a MS-DOS batch"},"content":{"rendered":"<div id=\"fr-FR\"><a href=\"#en-US\">English version<\/a><\/div>\n<p>Vous vous \u00eates d\u00e9j\u00e0 demand\u00e9 comment r\u00e9cup\u00e9rer le repertoire parent d&rsquo;un batch en MS-DOS et constatez que %cd% ne marchait pas correctement car il envoie votre repertoire courant et pas le r\u00e9pertoire du script (Faites un test en cr\u00e9ant un fichier test.cmd contenant le texte <em><strong>echo %cd%<\/strong><\/em> et appelez le depuis un autre repertoire\/disque en utilisant son chemin absolu. Cela renverra votre propre emplacement). La solution est le code suivant :<\/p>\n<pre class=\"brush: plain; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\nSET CURRENT_DIR=%~dp0 \r\nSET CURRENT_DIR=%CURRENT_DIR:~0,-1%\r\n\r\n<\/pre>\n<p>La premi\u00e8re ligne r\u00e9cup\u00e8re le chemin complet du r\u00e9pertoire avec le \\ de fin. La seconde ligne supprime ce \\ de fin.<\/p>\n<p>La preuve par l&rsquo;exemple :<\/p>\n<p><a href=\"http:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami.png\" rel=\"attachment wp-att-1105\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1105\" src=\"http:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami.png\" alt=\"whereami\" width=\"358\" height=\"133\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami.png 358w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami-300x111.png 300w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/><\/a><\/p>\n<p>Plus d&rsquo;informations <a href=\"https:\/\/blogs.msdn.microsoft.com\/jgalla\/2005\/06\/29\/manipulating-file-names-in-command-shell-script\/\" target=\"_blank\" rel=\"noopener noreferrer\">ici<\/a>.<\/p>\n<hr \/>\n<div id=\"en-US\"><a href=\"#fr-FR\">Version fran\u00e7aise<\/a><\/div>\n<p>You ever wondered how to recover the directory parent in a MS-DOS batch and see that %cd% does not work correctly because it sends your current directory and not the directory of the script (do a test by creating a file test.cmd containing the <strong>echo %cd%<\/strong> text and run it from another directory\/disk using its absolute path. This will return your own location). The solution is the following code:<\/p>\n<div id=\"scid:9D7513F9-C04C-4721-824A-2B34F0212519:5b18d16e-faec-4dc0-8bc8-21e6ad67205e\" class=\"wlWriterEditableSmartContent\" style=\"float: none; margin: 0px; padding: 0px;\">\n<div>\n<pre class=\"brush: plain; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\nSET CURRENT_DIR=%~dp0 \r\nSET CURRENT_DIR=%CURRENT_DIR:~0,-1%\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The first line retrieves the full path of the directory with the ending \\. The second line removes this ending \\.<\/p>\n<\/div>\n<\/div>\n<p>The proof:<\/p>\n<p><a href=\"http:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami.png\" rel=\"attachment wp-att-1105\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1105\" src=\"http:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami.png\" alt=\"whereami\" width=\"358\" height=\"133\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami.png 358w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2016\/07\/whereami-300x111.png 300w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/><\/a><\/p>\n<p>Further details <a href=\"https:\/\/docs.microsoft.com\/en-us\/archive\/blogs\/jgalla\/manipulating-file-names-in-command-shell-script\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>Laurent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>English version Vous vous \u00eates d\u00e9j\u00e0 demand\u00e9 comment r\u00e9cup\u00e9rer le repertoire parent d&rsquo;un batch en MS-DOS et constatez que %cd% ne marchait pas correctement car [&#8230;]<\/p>\n","protected":false},"author":2,"featured_media":2476,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[30],"class_list":["post-131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ms-dos","tag-ms-dos"],"_links":{"self":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":5,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":2785,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/131\/revisions\/2785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/media\/2476"}],"wp:attachment":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}